summaryrefslogtreecommitdiff
path: root/libopencl/nvopencl_wrapper.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libopencl/nvopencl_wrapper.cc')
-rw-r--r--libopencl/nvopencl_wrapper.cc53
1 files changed, 44 insertions, 9 deletions
diff --git a/libopencl/nvopencl_wrapper.cc b/libopencl/nvopencl_wrapper.cc
index e68dfca..b52354d 100644
--- a/libopencl/nvopencl_wrapper.cc
+++ b/libopencl/nvopencl_wrapper.cc
@@ -63,26 +63,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdarg.h>
+void vmyexit(int code, const char *str,va_list ap)
+{
+ char buffer[1024];
+ snprintf(buffer,1024,"GPGPU-Sim API: nvopencl_wrapper ERROR ** %s\n", str);
+ vprintf(buffer,ap);
+ if( code ) {
+ exit(code);
+ }
+}
+void myexit(int code, const char *str, ... )
+{
+ va_list ap;
+ va_start(ap,str);
+ vmyexit(code,str,ap);
+ va_end(ap);
+}
int main(int argc, const char **argv)
{
cl_context context;
cl_program pgm;
cl_int errcode;
+ cl_uint num_devices;
FILE *fp = fopen(argv[1],"r");
if ( fp == NULL ) exit(1);
fseek(fp,0,SEEK_END);
size_t source_length = ftell(fp);
- if ( source_length == 0 ) exit(2);
+ if ( source_length == 0 ) myexit(2,"OpenCL file is empty");
char *source = (char*)calloc(source_length+1,1);
fseek(fp,0,SEEK_SET);
fread(source,1,source_length,fp);
- context = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, &errcode);
- if ( errcode != CL_SUCCESS ) exit(3);
+ char buffer[1024];
+ cl_uint num_platforms;
+ cl_platform_id* platforms;
+
+ errcode = clGetPlatformIDs(0, NULL, &num_platforms);
+ if ( errcode != CL_SUCCESS ) myexit(1,"clGetPlatformaIDs returned %d",errcode);
+ if ( num_platforms == 0 ) myexit(2,"No OpenCL platforms found");
+ platforms = (cl_platform_id*)malloc(num_platforms * sizeof(cl_platform_id));
+ errcode = clGetPlatformIDs(num_platforms, platforms, NULL);
+ if ( errcode != CL_SUCCESS ) myexit(3,"clGetPlatformIDs returned %d",errcode);
+ errcode = clGetPlatformInfo(platforms[0], CL_PLATFORM_NAME, 1024, &buffer, NULL);
+ if ( errcode != CL_SUCCESS ) myexit(3,"clGetPlatformInfo returned %d",errcode);
+ printf("GPGPU-Sim OpenCL API: Generating PTX using OpenCL platform \'%s\'\n",buffer);
+ errcode = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices);
+ if ( errcode != CL_SUCCESS ) myexit(4,"clGetDeviceIDs returned %d",errcode);
+ cl_device_id *devices = (cl_device_id *)malloc(num_devices * sizeof(cl_device_id) );
+ errcode = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, num_devices, devices, NULL);
+ if ( errcode != CL_SUCCESS ) myexit(5,"clGetDeviceIDs returned %d",errcode);
+ context = clCreateContext(0, num_devices, devices, NULL, NULL, &errcode);
+ if ( errcode != CL_SUCCESS ) myexit(6,"clCreateContext returned %d",errcode);
pgm = clCreateProgramWithSource(context, 1, (const char **)&source, &source_length, &errcode);
- if ( errcode != CL_SUCCESS ) exit(4);
+ if ( errcode != CL_SUCCESS ) myexit(7,"clCreateProgramWithSource returned %d",errcode);
char options[4096];
unsigned n=0;
@@ -93,17 +129,16 @@ int main(int argc, const char **argv)
n+= 2;
}
errcode = clBuildProgram(pgm, 0, NULL, options, NULL, NULL);
- if ( errcode != CL_SUCCESS ) exit(5);
+ if ( errcode != CL_SUCCESS ) myexit(8,"clBuildProgram returned %d",errcode);
size_t nbytes1=0;
- cl_uint num_devices;
errcode = clGetProgramInfo(pgm,CL_PROGRAM_NUM_DEVICES,sizeof(cl_uint),&num_devices,&nbytes1);
- if ( errcode != CL_SUCCESS ) exit(6);
+ if ( errcode != CL_SUCCESS ) myexit(9,"clGetProgramInfo returned %d",errcode);
size_t nbytes2=0;
size_t *binary_sizes = (size_t*)calloc(num_devices,sizeof(size_t));
errcode = clGetProgramInfo(pgm,CL_PROGRAM_BINARY_SIZES,sizeof(size_t)*num_devices,binary_sizes,&nbytes2);
- if ( errcode != CL_SUCCESS ) exit(7);
+ if ( errcode != CL_SUCCESS ) myexit(10,"clGetProgramInfo returned %d",errcode);
unsigned char **binaries = (unsigned char**)calloc(num_devices,sizeof(unsigned char*));
size_t bytes_to_read = 0;
@@ -115,7 +150,7 @@ int main(int argc, const char **argv)
size_t nbytes3=0;
errcode = clGetProgramInfo(pgm,CL_PROGRAM_BINARIES,bytes_to_read,binaries,&nbytes3);
- if ( errcode != CL_SUCCESS ) exit(8);
+ if ( errcode != CL_SUCCESS ) myexit(11,"clGetProgramInfo returned %d",errcode);
fp = fopen(argv[2],"w");
fprintf(fp,"%s",binaries[0]);