Example #1
0
void  dq_nearest_neighbors
(
  long      n,
  Point*    pt,
  nn_array*  nn
)
{
  long   i, oct;
  void  check_nn( long, Point*, nn_array* );

  long   shear[4][4] = {
                         {1, -1,  0,  2}, 
                         {2,  0, -1,  1}, 
                         {1,  1, -2,  0}, 
                         {0,  2, -1, -1} 
                       };



_pt = pt;

  for( oct = 0;  oct < 4;  oct++ )
  {
    for( i = 0;   i < n;   i++ )
    {
      sheared[i].x = shear[oct][0]*pt[i].x + shear[oct][1]*pt[i].y;
      sheared[i].y = shear[oct][2]*pt[i].x + shear[oct][3]*pt[i].y;
      sorted[i] = i;
    }
    
    qsort( sorted, n, sizeof(long), compare_x );
    ne_sw_nearest_neighbors( 0, n, sheared, sorted, aux, oct, nn );
  }

#ifdef DEBUG
  check_nn( n, pt, nn );
#endif

}
Example #2
0
static void init_cl(void)
{
    cl_int status;
    cl_platform_id platform_id;
    cl_context context;
    cl_program program;

    char *kernel_src = malloc(10240);
    check_nn(kernel_src, "kernel_src");

    status = clGetPlatformIDs(1, &platform_id, &num_platforms);
    SOFT_CHECK_CL(status, "get platform ids");

    fprintf(stderr, "#platforms: %u\n", num_platforms);
    if (num_platforms == 0)
        return;

    static char info[4][128];
    status = clGetPlatformInfo(platform_id, CL_PLATFORM_PROFILE, 128, info[0], NULL);
    SOFT_CHECK_CL(status, "get platform profile");
    status = clGetPlatformInfo(platform_id, CL_PLATFORM_VERSION, 128, info[1], NULL);
    SOFT_CHECK_CL(status, "get platform version");
    status = clGetPlatformInfo(platform_id, CL_PLATFORM_NAME, 128, info[2], NULL);
    SOFT_CHECK_CL(status, "get platform name");
    status = clGetPlatformInfo(platform_id, CL_PLATFORM_VENDOR, 128, info[3], NULL);
    SOFT_CHECK_CL(status, "get platform vendor");

    fprintf(stderr, "profile: %s\n", info[0]);
    fprintf(stderr, "version: %s\n", info[1]);
    fprintf(stderr, "name: %s\n", info[2]);
    fprintf(stderr, "vendor: %s\n", info[3]);

    cl_context_properties *props = getContextProperties(platform_id);
    context = clCreateContextFromType(props, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
    SOFT_CHECK_CL(status, "create context");

    // create a command queue
    cl_device_id device_id;
    status = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
    SOFT_CHECK_CL(status, "get device ids");

    queue = clCreateCommandQueue(context, device_id, 0, &status);
    SOFT_CHECK_CL(status, "create command queue");

    // allocate memory objects
    mainOctCL = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, octTreeLength*sizeof(OctTreeNode), mainOctTree, &status);
    SOFT_CHECK_CL(status, "create buffer");

    glGenTextures(1, &texture);
    check_gl();
    glBindTexture(GL_TEXTURE_2D, texture);
    check_gl();

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_gl();
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_gl();

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
    check_gl();
    glFinish();
    check_gl();

    image = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, texture, &status);
    SOFT_CHECK_CL(status, "create image");

    // create the compute program
    FILE *kernel_handle = fopen("ray.cl", "rb");
    check_nn(kernel_handle, "fopen ray.cl");

    size_t n_bytes = fread(kernel_src, 1, 10239, kernel_handle);
    kernel_src[n_bytes] = '\0';
    check_ferror(kernel_handle, "fread");

    program = clCreateProgramWithSource(context, 1, (const char **)&kernel_src, NULL, &status);
    SOFT_CHECK_CL(status, "create program");

    // build the compute program executable
    status = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
    if (status != CL_BUILD_PROGRAM_FAILURE && status != CL_SUCCESS) {
        SOFT_CHECK_CL(status, "build program");
    } else {
        size_t log_size;
        status = clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
        SOFT_CHECK_CL(status, "get program build log size");

        char *log = malloc(log_size);
        check_nn(log, "log");

        status = clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
        SOFT_CHECK_CL(status, "get program build log");

        fprintf(stderr, "build kernel log:\n%s\n", log);
    }

    // create the compute kernel
    kernel = clCreateKernel(program, "ray_cl", &status);
    SOFT_CHECK_CL(status, "create kernel");

    status = clReleaseProgram(program);
    SOFT_CHECK_CL(status, "release program");

    status = clReleaseContext(context);
    SOFT_CHECK_CL(status, "release context");

    fprintf(stderr, "OpenCL initialization successful\n");
    render_method = TracerCL;
}