برنامه ای بنویسید که موارد زیر را برای CPU و GPU محاسبه کرده و زمان آنها را مقایسه کند:
برنامه ای بنویسید که موارد زیر را برای CPU و GPU محاسبه کرده و زمان آنها را مقایسه کند:
مثال شماره 2 درس چند پردازنده
این مثال و برخی از مثالهایی که در جلسات آینده طرح خواهد شد، از مثالهای شرکت AMD است که با تغییراتی در این درس طرح می شوند.
/********************************************************************** MultiCore Programming Hossein Khosravi Shahrood University of Technology ********************************************************************/ // For clarity,error checking has been omitted. #include <CL/cl.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string> #include <fstream> #define SUCCESS 0 #define FAILURE 1 using namespace std;
//برای دیدن کد کامل، ادامه مطلب را ببینید
مثال شماره 1 درس چند پردازنده
این مثال و برخی از مثالهایی که در جلسات آینده طرح خواهد شد، از مثالهای شرکت AMD است که با تغییراتی در این درس طرح می شوند.
// MultiCore Programming // Hossein Khosravi // Shahrood University of Technology #include "stdafx.h" // You might need to change this header based on your install: #include <CL/cl.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib, "OpenCl.lib") static void check_error(cl_int error, char* name) { if (error != CL_SUCCESS) { fprintf(stderr, "Non-successful return code %d for %s. Exiting.\n", error, name); exit(1); } } int main(int argc, char const *argv[]) { cl_uint i; cl_int err; // Discover the number of platforms: cl_uint nplatforms; err = clGetPlatformIDs(0, NULL, &nplatforms); check_error(err, "clGetPlatformIds"); // Now ask OpenCL for the platform IDs: cl_platform_id* platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id) * nplatforms); err = clGetPlatformIDs(nplatforms, platforms, NULL); check_error(err, "clGetPlatformIds"); // Ask OpenCL about each platform to understand the problem: char name[128]; char vendor[128]; char version[128]; fprintf(stdout, "OpenCL reports %d platforms.", nplatforms); cl_device_id devs[3]; for (i = 0; i < nplatforms; i++) { err |= clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, 128, vendor, NULL); err |= clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 128, name, NULL); err |= clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, 128, version, NULL); check_error(err, "clGetPlatformInfo"); fprintf(stdout, "\n********************\nPlatform %d: %s %s %s\n", i, vendor, name, version); cl_uint num_devices = 0; //find number of devices for each platform //status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices); err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices); check_error(err, "clGetDeviceIDs"); cl_device_id* devices = (cl_device_id*) malloc(sizeof(cl_device_id) * num_devices); clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL); for (int j = 0; j < num_devices; j++) { int maxComputeUnits = 0; // print parallel compute units clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(maxComputeUnits), &maxComputeUnits, NULL); printf("\n\tNum of compute units: %d\n", maxComputeUnits); cl_uint addr_data = 0; char name_data[100] = { 0 }, ext_data[4096] = { 0 }, version[100] = {0}; err = clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(name_data), name_data, NULL); err |= clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, sizeof(version), version, NULL); err |= clGetDeviceInfo(devices[j], CL_DEVICE_ADDRESS_BITS, sizeof(addr_data), &addr_data, NULL); err |= clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, sizeof(ext_data), ext_data, NULL); check_error(err, "clGetDeviceInfo"); printf("\tDevice #%d) NAME: %s\n\tVERSION: %s\n\tADDRESS_WIDTH: %u\n\tEXTENSIONS: %s\n\t----------\n", j, name_data, version, addr_data, ext_data); } free(devices); } getchar(); free(platforms); return 0; }
برای دیدن خروجی، ادامه مطلب را ببینید:
مثال مطرح شده در کلاس برای کار با رشته ها:
#include <stdlib.h> #include <stdio.h> #include <string.h> using namespace std; int main() { //khosravi.blog.ir char s1[20] = ""; char s2[20] = ""; char s3[40] = ""; printf("\nPlease enter 2 strings less than 20 characters\n"); scanf("%s", s1); scanf("%s", s2); if(strcmp(s1, s2) == 0) printf("\nTwo strings are equal"); else printf("\nTwo strings are not equal"); strcpy(s3, s1); strcat(s3, " "); strcat(s3, s2); printf("\ns3 = %s", s3); getchar(); return 0; }
float n1; printf("Please enter a number: "); scanf("%g", &n1);
printf("Sum of n1 and n2 is %g", n1+n2);
#include <stdio.h> int main() { // printf() displays the string inside quotation printf("Hello, World!"); return 0; }
Hello, World!
#include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // True if the number is perfectly divisible by 2 if(number % 2 == 0) printf("%d is even.", number); else printf("%d is odd.", number); return 0; }
Enter an integer: -7 -7 is odd.
توضیح: این مثال برای جابجایی دو عدد اعشاری (double) نوشته شده است. برای دریافت اعداد اعشاری از lf% استفاده می کنیم. اگر بخواهید برنامه را برای اعداد صحیح بنویسید، به جای double از int و به جای lf% از d% استفاده کنید.
#include <stdio.h> int main() { double firstNumber, secondNumber, temporaryVariable; printf("Enter first number: "); scanf("%lf", &firstNumber); printf("Enter second number: "); scanf("%lf",&secondNumber); // Value of firstNumber is assigned to temporaryVariable temporaryVariable = firstNumber; // Value of secondNumber is assigned to firstNumber firstNumber = secondNumber; // Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber secondNumber = temporaryVariable; printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber); printf("After swapping, secondNumber = %.2lf", secondNumber); return 0; }
Enter first number: 1.20 Enter second number: 2.45 After swapping, firstNumber = 2.45 After swapping, secondNumber = 1.20مثالها از سایت مثالهای C برداشته شده است و می توانید مثالهای بیشتری را آنجا مطالعه کنید.