void ocl_fini(ocl_t *ocl) { if (ocl) { clDeleteEnvironment(ocl->env); } }
int main(int argc, char *argv[]) { const cl_uint numBodies = 10; float *m = cl_malloc_array(float, numBodies); float *t = cl_malloc_array(float, numBodies); cl_float4 *a = cl_malloc_array(cl_float4, numBodies); cl_float4 *v = cl_malloc_array(cl_float4, numBodies); cl_float4 *p = cl_malloc_array(cl_float4, numBodies); #ifdef CL_BUILD_RUNTIME cl_uint type = clGetTypeFromString(CL_USER_DEVICE_TYPE); cl_uint count = CL_USER_DEVICE_COUNT; cl_environment_t *pEnv = clCreateEnvironment(KDIR"kernel_nbody.cl",type,count,notify,CL_ARGS); #else cl_environment_t *pEnv = clCreateEnvironmentFromBins(&gKernelBins, notify, CL_ARGS); #endif if (pEnv) { cl_uint i = 0, j = 0; cl_uint numIterations = (argc > 1?atoi(argv[1]):10); for (i = 0; i < numBodies; i++) { m[i] = frand() * ipow(10,rrand(4,27)); // masses should be 10^4 - 10^27 ("Earth heavy") frand4(a[i], 1, 3); frand4(v[i], 1, 2); frand4(p[i], 4, 8); t[i] = 0.001f; // 1 millisecond. } i = 0; for (j = 0; j < numIterations; j++) { nbodies(pEnv, m, a, v, p, t, numBodies); #if defined(DARWIN) printf("[%6u] p={%lf,%lf,%lf} v={%lf,%lf,%lf} a={%lf,%lf,%lf}\n", i, p[i][0], p[i][1], p[i][2], v[i][0], v[i][1], v[i][2], a[i][0], a[i][1], a[i][2]); #else printf("[%6u] p={%lf,%lf,%lf} v={%lf,%lf,%lf} a={%lf,%lf,%lf}\n", i, p[i].s[0], p[i].s[1], p[i].s[2], v[i].s[0], v[i].s[1], v[i].s[2], a[i].s[0], a[i].s[1], a[i].s[2]); #endif } clDeleteEnvironment(pEnv); cl_free(t); cl_free(m); cl_free(v); cl_free(a); cl_free(p); } return 0; }
int main(int argc, char *argv[]) { // initialize the default values numDevices = 1; verbose = CL_FALSE; no_print_logo = CL_FALSE; strncpy(filename, "kernels.cl", CL_MAX_PATHSIZE); //strncpy(outfile, "a.out", CL_MAX_PATHSIZE); memset(outfile, 0, sizeof(outfile)); //strncpy(precomp, "kernels.h", CL_MAX_PATHSIZE); memset(precomp, 0, sizeof(precomp)); if (argc > 1) { cl_environment_t *pEnv = NULL; cl_uint dev_type = CL_DEVICE_TYPE_DEFAULT; clOptions(argc, argv); print_logo(); if (verbose && filename[0] != '\0') printf("Compiling File: %s\n",filename); if (verbose && cl_args[0] != '\0') printf("CL ARGS: %s\n",cl_args); if (verbose && precomp[0] != '\0') printf("Precompiled Header: %s\n", precomp); dev_type = clGetTypeFromString(cl_device_types); if (verbose) printf("%u devices as type 0x%08x\n",numDevices,dev_type); // process the kernel pEnv = clCreateEnvironment(filename, dev_type, numDevices, notify, cl_args); if (pEnv == NULL) { printf("ERROR: Failed to compile %s\n", filename); } clDeleteEnvironment(pEnv); } else { cl_uint i; print_logo(); printf("Usage:\n$ %s [OPTIONS] \n", argv[0]); for (i = 0; i < numOpts; i++) { printf("Option %s\t\t%s\n",options[i].arg, options[i].description); } printf("Example:\n$ %s -f %s -d %u -o %s\n",argv[0],filename,numDevices,outfile); } return 0; }
int main(int argc, char *argv[]) { if (argc == 1) { const cl_uint width = 1920; const cl_uint height = 1080; const cl_uint numPixels = height * width; cl_int err = CL_SUCCESS; float *r = cl_malloc_array(float, numPixels); float *g = cl_malloc_array(float, numPixels); float *b = cl_malloc_array(float, numPixels); float *y = cl_malloc_array(float, numPixels); float *u = cl_malloc_array(float, numPixels); float *v = cl_malloc_array(float, numPixels); cl_uchar *Yp = cl_malloc_array(cl_uchar, numPixels); cl_uchar *Up = cl_malloc_array(cl_uchar, numPixels); cl_uchar *Vp = cl_malloc_array(cl_uchar, numPixels); float bt601[9] = {0.257f, 0.504f, 0.098f, -0.148f, -0.291f, 0.439f, 0.439f, -0.368f, -0.071f}; time_t start, diff; clock_t c_start, c_diff1, c_diff2; #ifdef CL_BUILD_RUNTIME cl_environment_t *pEnv = clCreateEnvironment(KDIR"kernel_yuv.cl",CL_DEVICE_TYPE_GPU,1,notify, CL_ARGS); #else cl_environment_t *pEnv = clCreateEnvironmentFromBins(&gKernelBins, notify, CL_ARGS); #endif printf("Processing %ux%u => %u pixels\n", width, height, numPixels); if (pEnv && r && g && b && y && u && v) { cl_uint i = 0; srand((unsigned int)time(NULL)); // initialize the data for (i = 0; i < numPixels; i++) { r[i] = frrand(0.0,1.0); // [0-1] g[i] = frrand(0.0,1.0); // [0-1] b[i] = frrand(0.0,1.0); // [0-1] y[i] = 0.00; u[i] = 0.00; v[i] = 0.00; } start = time(NULL); c_start = clock(); err = cl_convert_rgbf_to_yuvf_bt601(pEnv, r, g, b, y, u, v, numPixels); cl_assert(err == CL_SUCCESS,printf("Error = %d\n",err)); c_diff1 = clock() - c_start; diff = time(NULL) - start; printf("With Constants Version Ran in %lu seconds (%lu ticks)\n", diff, c_diff1); // initialize the data for (i = 0; i < numPixels; i++) { r[i] = frrand(0.0,1.0); // [0-1] g[i] = frrand(0.0,1.0); // [0-1] b[i] = frrand(0.0,1.0); // [0-1] y[i] = 0.00; u[i] = 0.00; v[i] = 0.00; } start = time(NULL); c_start = clock(); cl_convert_rgbf_to_yuvf(pEnv, r, g, b, y, u, v, bt601, numPixels); c_diff2 = clock() - c_start; diff = time(NULL) - start; printf("With No Constants Version Ran in %lu seconds (%lu ticks)\n", diff, c_diff2); normalize_float(y, 16, 235, Yp, numPixels); normalize_float(u, -128, 128, Up, numPixels); normalize_float(v, -128, 128, Vp, numPixels); #ifdef CL_DEBUG for (i = 0; i < numPixels; i++) printf("YUV = {0x%02x, 0x%02x, 0x%02x}\n", Yp[i], Up[i], Vp[i]); #endif clDeleteEnvironment(pEnv); } cl_free(r); cl_free(g); cl_free(b); cl_free(y); cl_free(u); cl_free(v); } else if (argc >= 6)
int main(int argc, char *argv[]) { if (argc >= 5) { FILE *fi = NULL; FILE *fo = NULL; cl_uint width = atoi(argv[2]); cl_uint height = atoi(argv[3]); cl_int err = CL_SUCCESS; #ifdef CL_BUILD_RUNTIME cl_environment_t *pEnv = clCreateEnvironment(KDIR"kernel_imgfilter.cl", CL_DEVICE_TYPE_GPU, 2, notify, CL_ARGS); #else cl_environment_t *pEnv = clCreateEnvironmentFromBins(&gKernelBins, notify, CL_ARGS); #endif if (!pEnv) { clDeleteEnvironment(pEnv); return -1; } clPrintAllKernelInfo(pEnv->kernels[0]); fi = fopen(argv[1], "rb"); fo = fopen(argv[4], "wb+"); if (fi && fo) { cl_char sobel[2][3][3] = { { {1,2,1}, {0,0,0}, {-1,-2,-1} }, { {-1,0,1}, {-2,0,2}, {-1,0,1} } }; size_t numBytes = width * height * sizeof(unsigned char); cl_uchar *input_image = (cl_uchar *)malloc(numBytes); cl_uchar *output_image = (cl_uchar *)malloc(numBytes); clock_t c_start, c_diff; cl_uint limit = 255; cl_uint range = range_of_operator((cl_char *)sobel, 3, limit); printf("Range of Sobel = %u, Limit = %u\n",range,limit); do { numBytes = fread(input_image, 1, numBytes, fi); if (numBytes == 0) break; c_start = clock(); err = imgfilter1d_opt(pEnv, width, height, input_image, width, output_image, width, (cl_char *)sobel, 3, range, limit); c_diff = clock() - c_start; printf("Sobel took %lu ticks\n", c_diff); numBytes = fwrite(output_image, 1, numBytes, fo); } while (err == CL_SUCCESS); free(input_image); free(output_image); fclose(fi); fclose(fo); } clDeleteEnvironment(pEnv); } else { printf("%s <filename> <width> <height> <outfile>\n",argv[0]); } return 0; }
int main(int argc, char *argv[]) { spacetime = (spacetime_t *)calloc(1, sizeof(spacetime_t)); if (spacetime) { char title[80]; // initialize the spacetime info spacetime->m_fps = 30; spacetime->m_body.m_mass = MASS_MOON; spacetime->m_body.m_radius = 2.0f; SUBREF(spacetime->m_body.m_position,X) = -GRID_DIM/2; SUBREF(spacetime->m_body.m_velocity,X) = 1; spacetime->m_camera.m_radius = 20.0f; spacetime->m_camera.m_theta = -M_PI/2; spacetime->m_camera.m_phi = M_PI/2; SUBREF(spacetime->m_camera.m_up,Z) = 1.0f; spacetime->m_width = 640; spacetime->m_height = 480; spacetime->m_camera.m_fov = 40.0f; spacetime->m_camera.m_ratio = (float)spacetime->m_width/spacetime->m_height; spacetime->m_camera.m_near = 1.0f; spacetime->m_camera.m_far = 7000.0f; // initialize the grid points (from -GRID_DIM/2 to GRID_DIM/2) for (int iz = 0; iz < GRID_DIM; iz++) { for (int iy = 0; iy < GRID_DIM; iy++) { for (int ix = 0; ix < GRID_DIM; ix++) { int i = (iz*GRID_DIM*GRID_DIM) + (iy*GRID_DIM) + (ix); SUBREF(spacetime->m_grid.m_position[i],X) = (cl_double)(ix - GRID_DIM/2); SUBREF(spacetime->m_grid.m_position[i],Y) = (cl_double)(iy - GRID_DIM/2); SUBREF(spacetime->m_grid.m_position[i],Z) = (cl_double)(iz - GRID_DIM/2); spacetime->m_grid.m_mass[i] = MASS_IOTA; } } } #ifdef CL_BUILD_RUNTIME spacetime->m_pEnv = clCreateEnvironment(KDIR "kernel_spacetime.cl", CL_DEVICE_TYPE_GPU, 1, ocl_notify, CL_ARGS); #else spacetime->m_pEnv = clCreateEnvironmentFromBins(&gKernelBins, ocl_notify, CL_ARGS); #endif if (!spacetime->m_pEnv) { printf("OpenCL Environment failure!\n"); free(spacetime); return -1; } sprintf(title,"Space-Time Warping Due to Mass; Simulation by Erik Rainey"); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE|GLUT_MULTISAMPLE); glutInitWindowPosition(100, 100); glutInitWindowSize(spacetime->m_width, spacetime->m_height); glutInit(&argc, argv); glutCreateWindow(title); // initialize the display settings glEnable(GL_CULL_FACE); glClearColor(0,0,0,0); // set the background color to the blackness of space ... ! // initialize each light for (size_t i = 0; i < dimof(spacetime->m_lights); i++) { GLfloat arr[4]; SUBREF(spacetime->m_lights[i].m_ambient,A) = 1.0f; SUBREF(spacetime->m_lights[i].m_diffuse,R) = 1.0f; SUBREF(spacetime->m_lights[i].m_diffuse,G) = 1.0f; SUBREF(spacetime->m_lights[i].m_diffuse,B) = 1.0f; SUBREF(spacetime->m_lights[i].m_diffuse,A) = 1.0f; SUBREF(spacetime->m_lights[i].m_position,X) = 20.0f; SUBREF(spacetime->m_lights[i].m_position,Y) = 20.0f; SUBREF(spacetime->m_lights[i].m_position,Z) = 20.0f; spacetime->m_lights[i].m_light = GL_LIGHT0 + ((GLenum)i); clftoglf(arr, spacetime->m_lights[i].m_ambient); glLightfv(spacetime->m_lights[i].m_light, GL_AMBIENT, arr); clftoglf(arr, spacetime->m_lights[i].m_diffuse); glLightfv(spacetime->m_lights[i].m_light, GL_DIFFUSE, arr); clftoglf(arr, spacetime->m_lights[i].m_position); glLightfv(spacetime->m_lights[i].m_light, GL_POSITION, arr); } // install the hooks glutDisplayFunc(glut_display); glutReshapeFunc(glut_reshape); glutIdleFunc(NULL); uint32_t ms = 1000 / (int)spacetime->m_fps; glutTimerFunc(ms,glut_timer,0); // give initial count as zero glutPassiveMotionFunc(glut_passive); glutKeyboardFunc(glut_keyfunc); // install the main loop, I don't think we return from this until killed glutMainLoop(); free(spacetime); clDeleteEnvironment(spacetime->m_pEnv); } return 0; }