Created
April 14, 2015 10:39
-
-
Save jrprice/abf644ef6032538655bf to your computer and use it in GitHub Desktop.
Simple command-line OpenCL C compiler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <CL/cl.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char *argv[]) | |
{ | |
cl_int err; | |
cl_platform_id platform; | |
cl_device_id device; | |
cl_context context; | |
cl_program program; | |
if (argc < 2) | |
{ | |
printf("Usage: ./compile source [DEVICE]\n"); | |
return 1; | |
} | |
char *filename = argv[1]; | |
int index = 0; | |
if (argc > 2) | |
index = atoi(argv[2]); | |
clGetPlatformIDs(1, &platform, NULL); | |
cl_uint num; | |
cl_device_id devices[3]; | |
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 3, devices, &num); | |
if (index >= num) | |
{ | |
printf("Invalid device index (%d).\n", index); | |
return 1; | |
} | |
device = devices[index]; | |
char name[256]; | |
clGetDeviceInfo(device, CL_DEVICE_NAME, 256, name, NULL); | |
printf("Using device: %s\n", name); | |
cl_device_fp_config cfg; | |
clGetDeviceInfo(device, CL_DEVICE_DOUBLE_FP_CONFIG, sizeof(cfg), &cfg, NULL); | |
printf("Double FP config = %llu\n", cfg); | |
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL); | |
FILE *file = fopen(filename, "r"); | |
if (!file) | |
{ | |
printf("Source file not found.\n"); | |
return 1; | |
} | |
char *source = malloc(65536); | |
fread(source, 1, 65536, file); | |
fclose(file); | |
program = clCreateProgramWithSource(context, 1, (const char**)&source, | |
NULL, NULL); | |
err = clBuildProgram(program, 1, &device, "", NULL, NULL); | |
if (err == CL_SUCCESS) | |
{ | |
printf("Program built successfully.\n"); | |
} | |
else | |
{ | |
if (err == CL_BUILD_PROGRAM_FAILURE) | |
{ | |
size_t sz; | |
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, | |
0, NULL, &sz); | |
char *log = malloc(++sz); | |
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, | |
sz, log, NULL); | |
printf("%s\n", log); | |
free(log); | |
} | |
else | |
{ | |
printf("Other build error\n"); | |
} | |
} | |
clReleaseProgram(program); | |
clReleaseContext(context); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment