/** * In case BOOST_COMPUTE_USE_OFFLINE_CACHE macro is defined, * the compiled binary is stored for reuse in the offline cache located in * $HOME/.boost_compute on UNIX-like systems and in %APPDATA%/boost_compute * on Windows. */ static program build_with_source_file( const std::string &file, const context &context, const std::string &options = std::string() ) { return build_with_source(read_source_file(file), context, options); }
static void maybe_print_source_line(char *file, int line) { if (!dumpsource) return; char **lines = map_get(source_lines, file); if (!lines) { lines = read_source_file(file); if (!lines) return; map_put(source_lines, file, lines); } int len = 0; for (char **p = lines; *p; p++) len++; emit_nostack("# %s", lines[line - 1]); }
int main(int argc, char *argv[]) { const char *srcfile, *dstfile; int src_is_jed, dst_is_jed; int numfuses = 0; jed_data jed; int len; int err; /* needs at least two arguments */ if (argc < 3) { fprintf(stderr, "Usage:\n" " jedutil <source.jed> <target.bin> [fuses] -- convert JED to binary form\n" " jedutil <source.bin> <target.jed> -- convert binary to JED form\n" ); return 0; } /* extract arguments */ srcfile = argv[1]; dstfile = argv[2]; if (argc >= 4) numfuses = atoi(argv[3]); /* does the source end in '.jed'? */ len = strlen(srcfile); src_is_jed = (srcfile[len - 4] == '.' && tolower(srcfile[len - 3]) == 'j' && tolower(srcfile[len - 2]) == 'e' && tolower(srcfile[len - 1]) == 'd'); /* does the destination end in '.jed'? */ len = strlen(dstfile); dst_is_jed = (dstfile[len - 4] == '.' && tolower(dstfile[len - 3]) == 'j' && tolower(dstfile[len - 2]) == 'e' && tolower(dstfile[len - 1]) == 'd'); /* error if neither or both are .jed */ if (!src_is_jed && !dst_is_jed) { fprintf(stderr, "At least one of the filenames must end in .jed!\n"); return 1; } if (src_is_jed && dst_is_jed) { fprintf(stderr, "Both filenames cannot end in .jed!\n"); return 1; } /* read the source file */ err = read_source_file(srcfile); if (err != 0) return 1; /* if the source is JED, convert to binary */ if (src_is_jed) { printf("Converting '%s' to binary form '%s'\n", srcfile, dstfile); /* read the JEDEC data */ err = jed_parse(srcbuf, srcbuflen, &jed); switch (err) { case JEDERR_INVALID_DATA: fprintf(stderr, "Fatal error: Invalid .JED file\n"); return 1; case JEDERR_BAD_XMIT_SUM: fprintf(stderr, "Fatal error: Bad transmission checksum\n"); return 1; case JEDERR_BAD_FUSE_SUM: fprintf(stderr, "Fatal error: Bad fusemap checksum\n"); return 1; } /* override the number of fuses */ if (numfuses != 0) jed.numfuses = numfuses; /* print out data */ printf("Source file read successfully\n"); printf(" Total fuses = %d\n", jed.numfuses); /* generate the output */ dstbuflen = jedbin_output(&jed, NULL, 0); dstbuf = malloc(dstbuflen); if (!dstbuf) { fprintf(stderr, "Unable to allocate %d bytes for the target buffer!\n", (int)dstbuflen); return 1; } dstbuflen = jedbin_output(&jed, dstbuf, dstbuflen); } /* if the source is binary, convert to JED */ else { printf("Converting '%s' to JED form '%s'\n", srcfile, dstfile); /* read the binary data */ err = jedbin_parse(srcbuf, srcbuflen, &jed); switch (err) { case JEDERR_INVALID_DATA: fprintf(stderr, "Fatal error: Invalid binary JEDEC file\n"); return 1; } /* print out data */ printf("Source file read successfully\n"); printf(" Total fuses = %d\n", jed.numfuses); /* generate the output */ dstbuflen = jed_output(&jed, NULL, 0); dstbuf = malloc(dstbuflen); if (!dstbuf) { fprintf(stderr, "Unable to allocate %d bytes for the target buffer!\n", (int)dstbuflen); return 1; } dstbuflen = jed_output(&jed, dstbuf, dstbuflen); } /* write the destination file */ err = write_dest_file(dstfile); if (err != 0) return 1; printf("Target file written succesfully\n"); return 0; }
/** * initialize OpenCL device */ int cl_init(int num_values, mvalue_ptr *values, int num_members, member *members, int metric_type) { int i, j; #ifdef _VERBOSE char string_one[128]; char string_two[128]; char string[256]; #endif // _VERBOSE int platform_index = 0; int device_index = 0; const char *source = NULL; population = num_members; segments = num_values; act_metric = metric_type; cl_int err; cl_uint platformCount; cl_uint deviceCount; cl_context_properties properties[3]; // Probe platforms clGetPlatformIDs(0, NULL, &platformCount); platforms = (cl_platform_id *) malloc(sizeof(cl_platform_id) * platformCount); clGetPlatformIDs(platformCount, platforms, NULL); #ifdef _VERBOSE for (i = 0; i < platformCount; i++) { printf("platform %d\n", i); // get all devices clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount); devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount); clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL); for (j = 0; j < deviceCount; j++) { clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 128, string_one, NULL); clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, 128, string_two, NULL); sprintf(string, "%s (version %s)", string_one, string_two); printf(" device %d: %s\n", j, string); } free(devices); } #endif // _VERBOSE if (platformCount == 0) { fprintf(stderr, "OpenCL platform not found\n"); return OPENCL_ERROR; } // ASK user do { #ifdef _VERBOSE puts("platform number: "); fgets((char *) string, 7, stdin); i = strtol(string, NULL, 10); #else i = 0; #endif } while (i >= platformCount); platform_index = i; // get all devices clGetDeviceIDs(platforms[platform_index], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount); devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount); clGetDeviceIDs(platforms[platform_index], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL); do { #ifdef _VERBOSE puts("device number: "); fgets((char *) string, 7, stdin); j = strtol(string, NULL, 10); #else j = 0; #endif } while (j >= deviceCount); device_index = j; // load values to dynamic memory for (i = 0; i < segments; i++) max_seg_vals = max_seg_vals > values[i].cvals ? max_seg_vals : values[i].cvals; mvalue *seg_vals = (mvalue *) malloc(sizeof(mvalue) * max_seg_vals * segments); memset(seg_vals, 0, sizeof(mvalue) * max_seg_vals * segments); // initialize for (i = 0; i < segments; i++) memcpy(seg_vals + i * max_seg_vals, values[i].vals, sizeof(mvalue) * values[i].cvals); // create lenghts array int *lenghts = (int *) malloc(sizeof(int) * segments); for (i = 0; i < segments; i++) lenghts[i] = values[i].cvals; // read kernels source = read_source_file("fitness.cl"); // context properties list - must be terminated with 0 properties[0]= CL_CONTEXT_PLATFORM; // specifies the platform to use properties[1]= (cl_context_properties) platforms[platform_index]; properties[2]= 0; // create context context = clCreateContext(properties,deviceCount,devices,NULL,NULL,&err); if (err != CL_SUCCESS) { printf("chyba ve vytváření kontextu %d\n", err); } // create command queue command_queue = clCreateCommandQueueWithProperties(context, devices[device_index], 0, &err); if (err != CL_SUCCESS) { printf("chyba ve vytváření fronty úloh %d\n", err); } program = clCreateProgramWithSource(context, 1, &source, 0, &err); err = clBuildProgram(program, 1, devices + device_index, "-I.", NULL, NULL); if (err != CL_SUCCESS) { // Determine the size of the log size_t log_size; clGetProgramBuildInfo(program, devices[device_index], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size); // Allocate memory for the log char *log = (char *) malloc(log_size); // Get the log clGetProgramBuildInfo(program, devices[device_index], CL_PROGRAM_BUILD_LOG, log_size, log, NULL); // Print the log printf("%s\n", log); free(log); clReleaseCommandQueue(command_queue); clReleaseContext(context); free(devices); free(platforms); return 1; } // specify which kernel from the program to execute kernel_population = clCreateKernel(program, "kernel_population", &err); kernel_equation = clCreateKernel(program, "solve_equation", &err); kernel_avg = clCreateKernel(program, "solve_avg", &err); free((void *) source); buf_seg_vals = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(mvalue) * max_seg_vals * segments, seg_vals, NULL); buf_lenghts = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(int) * segments, lenghts, NULL); buf_members = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(cl_float16) * population, members, NULL); buf_members_new = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(cl_float16) * population, members, NULL); buf_seg_vals_res = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float) * max_seg_vals * segments * population, NULL, NULL); free(seg_vals); free(lenghts); // set the argument list for the kernel command clSetKernelArg(kernel_population, 0, sizeof(cl_mem), &buf_members); clSetKernelArg(kernel_population, 1, sizeof(cl_mem), &buf_members_new); clSetKernelArg(kernel_equation, 0, sizeof(int), &segments); clSetKernelArg(kernel_equation, 1, sizeof(cl_mem), &buf_seg_vals); clSetKernelArg(kernel_equation, 2, sizeof(cl_mem), &buf_lenghts); clSetKernelArg(kernel_equation, 3, sizeof(int), &population); clSetKernelArg(kernel_equation, 4, sizeof(cl_mem), &buf_members_new); clSetKernelArg(kernel_equation, 5, sizeof(cl_mem), &buf_seg_vals_res); clSetKernelArg(kernel_equation, 6, sizeof(char), &act_metric); clSetKernelArg(kernel_avg, 0, sizeof(int), &max_seg_vals); clSetKernelArg(kernel_avg, 1, sizeof(int), &segments); clSetKernelArg(kernel_avg, 2, sizeof(cl_mem), &buf_seg_vals_res); clSetKernelArg(kernel_avg, 3, sizeof(cl_mem), &buf_lenghts); clSetKernelArg(kernel_avg, 4, sizeof(cl_mem), &buf_members); clSetKernelArg(kernel_avg, 5, sizeof(cl_mem), &buf_members_new); clSetKernelArg(kernel_avg, 6, sizeof(char), &act_metric); three_dim[0] = max_seg_vals; three_dim[1] = segments; three_dim[2] = population; one_dim[0] = population; return 0; }
/// Creates a new program with \p file in \p context. /// /// \see_opencl_ref{clCreateProgramWithSource} static program create_with_source_file(const std::string &file, const context &context) { // create program return create_with_source(read_source_file(file), context); }