2 Replies Latest reply on Apr 21, 2011 4:26 AM by hayabusaxps

    Unhandled exception at 0x0000000180001ce7

    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

        • Unhandled exception at 0x0000000180001ce7
          hayabusaxps


          #ifndef TEMPLATE_H_
          #define TEMPLATE_H_




          #include <CL/cl.h>
          #include <string.h>
          #include <cstdlib>
          #include <iostream>
          #include <string>
          #include <fstream>


          /*** GLOBALS ***/

          /*
           * Input data is stored here.
           This declares input as a pointer to an cl_integer.
          int a = 5;
          int *money = NULL;
          money = &a;
          Once a pointer has been declared, the next logical step is for it to point at something:
          **********************************
          *** Address     Contents       ***
          *** 0x8130         0x00000008     ***
          *** 0x8134       0x00008130     ***
          **********************************

          Then by dereferencing money by coding
          *money = 8;

          **********************************
          *** Address     Contents       ***
          *** 0x8130         0x00000008     ***
          *** 0x8134       0x00008130     ***
          **********************************
           */
          cl_float *input0;
          cl_uint                    x0;      /**< width of input Array */
          cl_uint                    y0;      /**< height of input Array */
          cl_uint                    z0;      /**< depth of input Array */
          cl_uint                    c0;      /**< components of input Array */


          cl_float *output0;

          /* The memory buffer that is used as input/output for OpenCL kernel */
          cl_mem   input0Buffer;
          cl_mem   output0Buffer;


          cl_context          context;
          cl_device_id        *devices;
          cl_command_queue    commandQueue;

          cl_program program;

          /* This program uses only one kernel and this serves as a handle to it */
          cl_kernel  kernel;


          /*** FUNCTION DECLARATIONS ***/
          /*
           * OpenCL related initialisations are done here.
           * Context, Device list, Command Queue are set up.
           * Calls are made to set up OpenCL memory buffers that this program uses
           * and to load the programs into memory and get kernel handles.
           */
          int initializeCL(void);

          /*
           *
           */
          std::string convertToString(const char * filename);

          /*
           * This is called once the OpenCL context, memory etc. are set up,
           * the program is loaded into memory and the kernel handles are ready.
           *
           * It sets the values for kernels' arguments and enqueues calls to the kernels
           * on to the command queue and waits till the calls have finished execution.
           *
           * It also gets kernel start and end time if profiling is enabled.
           */
          int runCLKernels(void);

          /* Releases OpenCL resources (Context, Memory etc.) */
          int cleanupCL(void);

          /* Releases program's resources */
          void cleanupHost(void);

          /*
           * Prints no more than 256 elements of the given array.
           * Prints full array if length is less than 256.
           *
           * Prints Array name followed by elements.
           */
          void print1DArray(
                   const std::string arrayName,
                   const unsigned int *arrayData,
                   const unsigned int length);


          #endif  /* #ifndef TEMPLATE_H_ */

            • Unhandled exception at 0x0000000180001ce7
              hayabusaxps

              /*!
               * Sample kernel which multiplies every element of the input array with
               * a constant and stores it at the corresponding output array
               */


              __kernel void templateKernel(__global  float *input0,
                                           __global  float *output0)
              {
                 
                 
                  int globalPos =  get_global_id(0)*get_global_size(1)* get_global_size(2) + get_global_id(1)*get_global_size(2) +get_global_id(2);
                 

                  output0[globalPos]= 1;

              }