Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis12-cl/src/lib/cl-target/ocl-image.c
ViewVC logotype

View of /branches/vis12-cl/src/lib/cl-target/ocl-image.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3118 - (download) (as text) (annotate)
Mon Mar 23 18:05:45 2015 UTC (4 years, 2 months ago) by jhr
File size: 8226 byte(s)
  working on OpenCL issues
/*! \file ocl-image.c
 *
 * \author John Reppy
 *
 * Support for loading 
 */

/*
 * COPYRIGHT (c) 2015 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#ifndef DIDEROT_TARGET_CL
#  define DIDEROT_TARGET_CL
#endif

#include "Diderot/Diderot.h"
#include "Diderot/shadow-types.h"

// Report an OpenCL error
STATIC_INLINE void ReportOCLError (OCLWorldPrefix_t *wrld, cl_int sts, const char *msg)
{
    biffMsgAddf (wrld->errors, "%s: %s", msg, Diderot_OCLErrorString(sts));
}

// Check the return status of a CL API call.  If there is an error, add a message to
// the biff buffer and return true, otherwise return false.
STATIC_INLINE bool CheckOCLStatus (OCLWorldPrefix_t *wrld, cl_int sts, const char *msg)
{
    if (sts != CL_SUCCESS) {
        ReportOCLError (wrld, sts, msg);
        return true;
    } 
    else
	return false;
}

//! load a 1D image into a GPU-side global.
//! \param wrld the world
//! \param globals the GPU-side memory object that holds the program globals
//! \param img the name of the image nrrd file
//! \param initKern the kernel that initializes the GPU-side global image in question
//! \param obj the GPU-side memory object that is allocated to hold the image data.
//! \return true if there are any errors
//
bool Diderot_LoadGPUImage1D (OCLWorldPrefix_t *wrld, cl_mem globals, const char *imgName, cl_kernel initKern, cl_mem *objOut)
{
    Diderot_image1D_t *img;
    Shadow_image1D_t shadow;
    cl_int sts;

    if (Diderot_LoadImage1D((WorldPrefix_t *)wrld, imgName, &img)) {
        return true;
    }

  // initialize the shadow object
    shadow.size[0] = img->size[0];
    shadow.s = img->s;
    shadow.t = img->t;

  // allocate and initialize GPU-side image object
    cl_mem imgObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, sizeof(shadow), &shadow, &sts);
    if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image")) {
	Diderot_FreeImage1D((WorldPrefix_t *)wrld, img);
	return true;
    }

  // allocate and initialize GPU-side image data
    cl_mem dataObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, img->dataSzb, img->data, &sts);
    if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image data")) {
	Diderot_FreeImage1D((WorldPrefix_t *)wrld, img);
	clReleaseMemObject(imgObj);
	return true;
    }

  /* invoke the initialization kernel. s*/
    if (((sts = clSetKernelArg (initKern, 0, sizeof(cl_mem), &globals)) != CL_SUCCESS)
    ||  ((sts = clSetKernelArg (initKern, 1, sizeof(cl_mem), &imgObj)) != CL_SUCCESS)
    ||  ((sts = clSetKernelArg (initKern, 2, sizeof(cl_mem), &dataObj)) != CL_SUCCESS))
    {
	ReportOCLError (wrld, sts, "error enqueuing image initialization kernel");
	Diderot_FreeImage1D((WorldPrefix_t *)wrld, img);
	clReleaseMemObject(imgObj);
	clReleaseMemObject(dataObj);
	return true;
    }

  // free CPU-side data
    Diderot_FreeImage1D((WorldPrefix_t *)wrld, img);

  // free GPU-side shadow object
    if (clReleaseMemObject(imgObj) != CL_SUCCESS) {
	ReportOCLError (wrld, sts, "error freeing output kernel");
	clReleaseMemObject(dataObj);
	return true;
    }

  // return data object for future release
    *objOut = dataObj;

    return false;

} // Diderot_LoadGPUImage1D

//! load a 2D image into a GPU-side global.
//! \param wrld the world
//! \param globals the GPU-side memory object that holds the program globals
//! \param img the name of the image nrrd file
//! \param initKern the kernel that initializes the GPU-side global image in question
//! \param obj the GPU-side memory object that is allocated to hold the image data.
//! \return true if there are any errors
//
bool Diderot_LoadGPUImage2D (OCLWorldPrefix_t *wrld, cl_mem globals, const char *imgName, cl_kernel initKern, cl_mem *objOut)
{
    Diderot_image2D_t *img;
    Shadow_image2D_t shadow;
    cl_int sts;

    if (Diderot_LoadImage2D((WorldPrefix_t *)wrld, imgName, &img)) {
        return true;
    }

  // initialize the shadow object
    for (int i = 0;  i < 2;  i++) {
	shadow.size[i] = img->size[i];
	ShadowVec2 (&(shadow.w2i[i]), img->w2i[i].v);
	ShadowVec2 (&(shadow.w2iT[i]), img->w2iT[i].v);
    }
    ShadowVec2 (&shadow.tVec, img->tVec);

  // allocate and initialize GPU-side image object
    cl_mem imgObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, sizeof(shadow), &shadow, &sts);
    if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image")) {
	Diderot_FreeImage2D((WorldPrefix_t *)wrld, img);
	return true;
    }

  // allocate and initialize GPU-side image data
    cl_mem dataObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, img->dataSzb, img->data, &sts);
    if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image data")) {
	Diderot_FreeImage2D((WorldPrefix_t *)wrld, img);
	clReleaseMemObject(imgObj);
	return true;
    }

  /* invoke the initialization kernel. s*/
    if (((sts = clSetKernelArg (initKern, 0, sizeof(cl_mem), &globals)) != CL_SUCCESS)
    ||  ((sts = clSetKernelArg (initKern, 1, sizeof(cl_mem), &imgObj)) != CL_SUCCESS)
    ||  ((sts = clSetKernelArg (initKern, 2, sizeof(cl_mem), &dataObj)) != CL_SUCCESS))
    {
	ReportOCLError (wrld, sts, "error enqueuing image initialization kernel");
	Diderot_FreeImage2D((WorldPrefix_t *)wrld, img);
	clReleaseMemObject(imgObj);
	clReleaseMemObject(dataObj);
	return true;
    }

  // free CPU-side data
    Diderot_FreeImage2D((WorldPrefix_t *)wrld, img);

  // free GPU-side shadow object
    if (clReleaseMemObject(imgObj) != CL_SUCCESS) {
	ReportOCLError (wrld, sts, "error freeing output kernel");
	clReleaseMemObject(dataObj);
	return true;
    }

  // return data object for future release
    *objOut = dataObj;

    return false;

} // Diderot_LoadGPUImage2D

//! load a 3D image into a GPU-side global.
//! \param wrld the world
//! \param globals the GPU-side memory object that holds the program globals
//! \param img the name of the image nrrd file
//! \param initKern the kernel that initializes the GPU-side global image in question
//! \param obj the GPU-side memory object that is allocated to hold the image data.
//! \return true if there are any errors
//
bool Diderot_LoadGPUImage3D (OCLWorldPrefix_t *wrld, cl_mem globals, const char *imgName, cl_kernel initKern, cl_mem *objOut)
{
    Diderot_image3D_t *img;
    Shadow_image3D_t shadow;
    cl_int sts;

    if (Diderot_LoadImage3D((WorldPrefix_t *)wrld, imgName, &img)) {
        return true;
    }

  // initialize the shadow object
    for (int i = 0;  i < 3;  i++) {
	shadow.size[i] = img->size[i];
	ShadowVec3 (&(shadow.w2i[i]), img->w2i[i].v);
	ShadowVec3 (&(shadow.w2iT[i]), img->w2iT[i].v);
    }
    ShadowVec3 (&shadow.tVec, img->tVec);

  // allocate and initialize GPU-side image object
    cl_mem imgObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, sizeof(shadow), &shadow, &sts);
    if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image")) {
	Diderot_FreeImage3D((WorldPrefix_t *)wrld, img);
	return true;
    }

  // allocate and initialize GPU-side image data
    cl_mem dataObj = clCreateBuffer(wrld->context, CL_MEM_READ_ONLY, img->dataSzb, img->data, &sts);
    if (CheckOCLStatus (wrld, sts, "error allocating GPU memory for image data")) {
	Diderot_FreeImage3D((WorldPrefix_t *)wrld, img);
	clReleaseMemObject(imgObj);
	return true;
    }

  /* invoke the initialization kernel. s*/
    if (((sts = clSetKernelArg (initKern, 0, sizeof(cl_mem), &globals)) != CL_SUCCESS)
    ||  ((sts = clSetKernelArg (initKern, 1, sizeof(cl_mem), &imgObj)) != CL_SUCCESS)
    ||  ((sts = clSetKernelArg (initKern, 2, sizeof(cl_mem), &dataObj)) != CL_SUCCESS))
    {
	ReportOCLError (wrld, sts, "error enqueuing image initialization kernel");
	Diderot_FreeImage3D((WorldPrefix_t *)wrld, img);
	clReleaseMemObject(imgObj);
	clReleaseMemObject(dataObj);
	return true;
    }

  // free CPU-side data
    Diderot_FreeImage3D((WorldPrefix_t *)wrld, img);

  // free GPU-side shadow object
    if (clReleaseMemObject(imgObj) != CL_SUCCESS) {
	ReportOCLError (wrld, sts, "error freeing output kernel");
	clReleaseMemObject(dataObj);
	return true;
    }

  // return data object for future release
    *objOut = dataObj;

    return false;

} // Diderot_LoadGPUImage3D

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0