مثال شماره 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;
}
برای دیدن خروجی، ادامه مطلب را ببینید: