hayabusaxps

Unhandled exception at 0x0000000180001ce7

Discussion created by hayabusaxps on Apr 21, 2011
Latest reply on Apr 21, 2011 by hayabusaxps

im not sure what im doing wrong would someone mind taking a look at what i have

 

right now im just trying to get a 3 dimensional input to kernal to output

 

/* ============================================================

Copyright (c) 2009 Advanced Micro Devices, Inc.  All rights reserved.
 
Redistribution and use of this material is permitted under the following
conditions:
 
Redistributions must retain the above copyright notice and all terms of this
license.
 
In no event shall anyone redistributing or accessing or using this material
commence or participate in any arbitration or legal action relating to this
material against Advanced Micro Devices, Inc. or any copyright holders or



#include "MRI_Template.hpp"
#include


/*
*    read in a bitmap file
*
*
*/






/*
 * \brief Host Initialization
 *        Allocate and initialize memory
 *        on the host. Print input array.
 */

//int initializeHost(void)
int initializeHost(void)
{
    x0                = 2;    //    width
    y0                = 2;    //    height
    z0                = 2;    //    depth
//    c0                = 4;    //    components

    input0                = NULL;
    output0                = NULL;


    /////////////////////////////////////////////////////////////////
    // Allocate and initialize memory used by host
    /////////////////////////////////////////////////////////////////
    cl_uint sizeInBytes0 = x0 * y0 *z0 * sizeof(cl_float);

    input0 = (cl_float *) malloc(sizeInBytes0);
    output0 = (cl_float *) malloc(sizeInBytes0);
    if(input0 == NULL)
    {
        std::cout<<"Error: Failed to allocate input memory on host\n";
        return 1;
    }



    // create INPUTS

    for(cl_uint i = 0; i < x0; i++)
        for(cl_uint j = 0; j < y0; j++)
            for(cl_uint k = 0; k < z0; k++)
        {

            int index = i*y0*z0 + j*z0 + k;

            cl_float indexval = (cl_float)index +1;
               
            input0[index] = indexval;


        }


    //    test input values

 for(cl_uint i = 0; i < x0; i++)
        for(cl_uint j = 0; j < y0; j++)
            for(cl_uint k = 0; k < z0; k++)

        {

            int index = i*y0*z0 + j*z0 + k;

            printf ("array index: %u   array value: %f \n",index ,input0[index]);

        }







    // print input array
    // print1DArray(std::string("Input").c_str(), input0, width);

    return 0;
}

/*
 * Converts the contents of a file into a string
 */
std::string convertToString(const char *filename)
{
    size_t size;
    char*  str;
    std::string s;

    std::fstream f(filename, (std::fstream::in | std::fstream::binary));

    if(f.is_open())
    {
        size_t fileSize;
        f.seekg(0, std::fstream::end);
        size = fileSize = (size_t)f.tellg();
        f.seekg(0, std::fstream::beg);

        str = new char[size+1];
        if(!str)
        {
            f.close();
            return NULL;
        }

        f.read(str, fileSize);
        f.close();
        str[size] = '\0';
   
        s = str;
        delete[] str;
        return s;
    }
    else
    {
        std::cout << "\nFile containg the kernel code(\".cl\") not found. Please copy the required file in the folder containg the executable.\n";
        exit(1);
    }
    return NULL;
}

/*
 * \brief OpenCL related initialization
 *        Create Context, Device list, Command Queue
 *        Create OpenCL memory buffer objects
 *        Load CL file, compile, link CL source
 *          Build program and kernel objects
 */
int initializeCL(void)
{
    cl_int status = 0;
    size_t deviceListSize;
    // cl_uint num_entries = 2;    // number of platforms for get platform ids

    /*
     * Have a look at the available platforms and pick either
     * the AMD one if available or a reasonable default.
     */

    cl_uint numPlatforms;
    cl_platform_id platform = NULL;
   
    // Obtain the list of platforms available
    /*
    cl_int clGetPlatformIDs(         // returns CL_SUCCESS is successful
    cl_uint num_entries,            // number of platform entries that can be added to platforms (If platforms is not NULL, the num_entries must be greater than zero. )
      cl_platform_id *platforms,        // (Returns a list of OpenCL platforms found.)
      cl_uint *num_platforms)
    */
    //status = clGetPlatformIDs(0, NULL, &numPlatforms);    // this will retrieve the number of platforms and store it in numPlatforms



   
    status = clGetPlatformIDs(0, NULL, &numPlatforms);
    if(status != CL_SUCCESS)    // test for success
    {
         std::cout << "Error: no platforms found \n";
        return 1;
    }
   

    //std::cout << "Error: Getting Platform Ids. (clGetPlatformsIDs)\n";


    printf ("number of platforms: %u \n", numPlatforms);

    if(numPlatforms > 0)
    {
        cl_platform_id *platforms = new cl_platform_id[numPlatforms];    // add the platform found to the platforms list
        status = clGetPlatformIDs(numPlatforms, platforms, NULL);
        if(status != CL_SUCCESS)
        {
            std::cout << "Error: Getting Platform Ids. (clGetPlatformsIDs)\n";
            return 1;
        }


        for(unsigned int i=0; i < numPlatforms; ++i)
        {
            char pbuff[100];
            status = clGetPlatformInfo(
                        platforms,
                        CL_PLATFORM_VENDOR,
                        sizeof(pbuff),
                        pbuff,
                        NULL);
            if(status != CL_SUCCESS)
            {
                std::cout << "Error: Getting Platform Info.(clGetPlatformInfo)\n";
                return 1;
            }
            platform = platforms
;
            if(!strcmp(pbuff, "Advanced Micro Devices, Inc."))
            {
                break;
            }
        }
        delete platforms;
    }

    if(NULL == platform)
    {
        std::cout << "NULL platform found so Exiting Application." << std::endl;
        return 1;
    }

    /*
    cl_int clGetDeviceIDs(     cl_platform_id platform,
      cl_device_type device_type,
      cl_uint num_entries,
      cl_device_id *devices,
      cl_uint *num_devices)
    */

    cl_device_type device_type = CL_DEVICE_TYPE_GPU;
    cl_uint num_devices;


    status = clGetDeviceIDs(platform, device_type,
      0,
      NULL,
      &num_devices);
    if(status != CL_SUCCESS)    // test for success
    {

        return 1;
    }
   
    printf ("number of devices: %u \n", num_devices);



    /*
     * If we could find our platform, use it. Otherwise use just available platform.
     */
    cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };

    /////////////////////////////////////////////////////////////////
    // Create an OpenCL context
    /////////////////////////////////////////////////////////////////
    context = clCreateContextFromType(cps,
                                      CL_DEVICE_TYPE_GPU,
                                      NULL,
                                      NULL,
                                      &status);
    if(status != CL_SUCCESS)
    { 
        std::cout<<"Error: Creating Context. (clCreateContextFromType)\n";
        return 1;
    }

    /* First, get the size of device list data */
    status = clGetContextInfo(context,
                              CL_CONTEXT_DEVICES,
                              0,
                              NULL,
                              &deviceListSize);
    if(status != CL_SUCCESS)
    { 
        std::cout<<
            "Error: Getting Context Info \
            (device list size, clGetContextInfo)\n";
        return 1;
    }

    /////////////////////////////////////////////////////////////////
    // Detect OpenCL devices
    /////////////////////////////////////////////////////////////////
    devices = (cl_device_id *)malloc(deviceListSize);
    if(devices == 0)
    {
        std::cout<<"Error: No devices found.\n";
        return 1;
    }

    /* Now, get the device list data */
    status = clGetContextInfo(
                 context,
                 CL_CONTEXT_DEVICES,
                 deviceListSize,
                 devices,
                 NULL);
    if(status != CL_SUCCESS)
    {
        std::cout<<
            "Error: Getting Context Info \
            (device list, clGetContextInfo)\n";
        return 1;
    }

    /////////////////////////////////////////////////////////////////
    // Create an OpenCL command queue
    /////////////////////////////////////////////////////////////////
    commandQueue = clCreateCommandQueue(
                       context,
                       devices[0],
                       0,
                       &status);
    if(status != CL_SUCCESS)
    {
        std::cout<<"Creating Command Queue. (clCreateCommandQueue)\n";
        return 1;
    }

    /////////////////////////////////////////////////////////////////
    // Create OpenCL memory buffers
    /////////////////////////////////////////////////////////////////
    input0Buffer = clCreateBuffer(
                      context,
                      CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
                      sizeof(cl_float) * x0 * y0 * z0,
                      input0,
                      &status);
    if(status != CL_SUCCESS)
    {
        std::cout<<"Error: clCreateBuffer (inputBuffer)\n";
        return 1;
    }

   
    /* Write data to buffer */
    /*
    status = clEnqueueWriteBuffer(commandQueue,
                                  input0Buffer,
                                  1,
                                  0,
                                  sizeof(cl_float) * x0 * y0 * z0,
                                  input0,
                                  0,
                                  0,
                                  0);
   
    */

    output0Buffer = clCreateBuffer(
                      context,
                      CL_MEM_WRITE_ONLY,
                      sizeof(cl_float) * x0 * y0 * z0,
                      0,
                      &status);
    if(status != CL_SUCCESS)
    {
        std::cout<<"Error: clCreateBuffer (outputBuffer)\n";
        return 1;
    }


   
   
    /////////////////////////////////////////////////////////////////
    // Load CL file, build CL program object, create CL kernel object
    /////////////////////////////////////////////////////////////////
    const char * filename  = "Template_Kernels.cl";
    std::string  sourceStr = convertToString(filename);
    const char * source    = sourceStr.c_str();
    size_t sourceSize[]    = { strlen(source) };

    program = clCreateProgramWithSource(
                  context,
                  1,
                  &source,
                  sourceSize,
                  &status);
    if(status != CL_SUCCESS)
    {
      std::cout<<
               "Error: Loading Binary into cl_program \
               (clCreateProgramWithBinary)\n";

        if(status = CL_INVALID_CONTEXT )
      std::cout<<
               "Error: CL_INVALID_CONTEXT \n";
        if(status = CL_INVALID_VALUE )
      std::cout<<
               "Error: CL_INVALID_VALUE \n";
        if(status = CL_OUT_OF_RESOURCES )
      std::cout<<
               "Error: CL_OUT_OF_RESOURCES \n";
        if(status = CL_OUT_OF_HOST_MEMORY )
      std::cout<<
               "Error: CL_OUT_OF_HOST_MEMORY \n";




      return 1;
    }






    /* create a cl program executable for

Outcomes