Ejemplo n.º 1
0
/*! Used to play a 3d sound
 *
 *  Note, if you call this on a file that has not been Loaded
 *  (see icSoundDeviceAL::LoadSource), it will keep this audio
 *  loaded in memory until you call UnloadSource or the device
 *  is destroyed.
 *
 *  @param          szFile          Audio file to play
 *  @param          soundParam      How to play that audio
 *  @param[out]     ppStream        Handle to the sound object
 *  @returns        ICRESULT        Success/failure of playing audio
**/
ICRESULT icSoundDeviceAL::Play3D(const char* szName,
                                 const icSoundParam& soundParams,
                                 icSoundI** ppSound)
{
    icSoundI* sound = GetFreeSound();
    if (!sound)
        return IC_FAIL_GEN;

    icSoundBuffer pBuf;

    if (ICEFAIL(FindSource(szName,pBuf)))
        LoadSource(szName);

    if (ICEFAIL(FindSource(szName,pBuf)))
        return IC_FAIL_GEN;


    icSoundAL* alSound = (icSoundAL*)sound;

    alSound->m_buf = pBuf;

    alSound->m_Params = soundParams;

    // assign the buffer to this source
    alSourcei(alSound->m_ALsource, AL_BUFFER, pBuf.buffID);

    alSound->Play3d();

    *ppSound = sound;

    return IC_OK;
}// END FUNCTION Play3D(const char* szName,
Ejemplo n.º 2
0
// Reads and parses the file
// and returns the created Book
QSharedPointer<Book> ImportHTML::GetBook()
{
    QString source = LoadSource();
    LoadMetadata(source);
    UpdateFiles(CreateHTMLResource(), source, LoadFolderStructure(source));
    return m_Book;
}
Ejemplo n.º 3
0
GLuint LoadShader(GLenum type, const char *filename)
{
    GLuint shader = 0;
    GLsizei logsize = 0;
    GLint compile_status = GL_TRUE;
    char *log = NULL;
    char *src = NULL;
    
    /* creation d'un shader de sommet */
    shader = glCreateShader(type);
    if(shader == 0)
    {
        fprintf(stderr, "impossible de creer le shader\n");
        return 0;
    }
    
    /* chargement du code source */
    src = LoadSource(filename);
    if(src == NULL)
    {    
        glDeleteShader(shader);
        return 0;
    }
    
    /* assignation du code source */
    glShaderSource(shader, 1, (const GLchar**)&src, NULL);
    
    /* compilation du shader */
    glCompileShader(shader);
    
    /* liberation de la memoire du code source */
    free(src);
    src = NULL;
    
    /* verification du succes de la compilation */
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
    if(compile_status != GL_TRUE)
    {
        
        
        /* on recupere la taille du message d'erreur */
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logsize);
        
        /* on alloue un espace memoire dans lequel OpenGL ecrira le message */
        log =(char*) malloc(logsize + 1);
      
        memset(log, '\0', logsize + 1);
        
        glGetShaderInfoLog(shader, logsize, &logsize, log);
        fprintf(stderr, "impossible de compiler le shader '%s' :\n%s", filename, log);
        
        free(log);
        glDeleteShader(shader);
        
        return 0;
    }
    
    return shader;
}
Ejemplo n.º 4
0
status_t
Keymap::LoadSource(const char* name)
{
	FILE* file = fopen(name, "r");
	if (file == NULL)
		return errno;

	status_t status = LoadSource(file);
	fclose(file);

	return status;
}
Ejemplo n.º 5
0
// Reads and parses the file
// and returns the created Book
QSharedPointer<Book> ImportHTML::GetBook(bool extract_metadata)
{
    QString source = LoadSource();
    if (extract_metadata) {
        LoadMetadata(source);
    }
    UpdateFiles(CreateHTMLResource(), source, LoadFolderStructure(source));

    // Before returning the new book, if it is epub3, make sure it has a nav
    if (m_EpubVersion.startsWith('3')) {
        HTMLResource* nav_resource = m_Book->GetConstOPF()->GetNavResource();
        if (!nav_resource) {
            HTMLResource * nav_resource = m_Book->CreateEmptyNavFile(true);
            m_Book->GetOPF()->SetNavResource(nav_resource);
            m_Book->GetOPF()->SetItemRefLinear(nav_resource, false);
        }
    }
    return m_Book;
}
Ejemplo n.º 6
0
/*! Gets a handle to a sound for playing
 *
 *  @param          szFile          Audio file to play
 *  @param[out]     ppStream        Handle to the sound object
 *  @returns        ICRESULT        Success/failure of playing audio
**/
ICRESULT icSoundDeviceAL::GetSound(const char* szName, icSoundI** ppSound)
{
    icSoundI* sound = GetFreeSound();
    if (!sound)
        return IC_FAIL_GEN;

    icSoundBuffer pBuf;
    
    if (ICEFAIL(FindSource(szName,pBuf)))
	{
		if (ICEFAIL(LoadSource(szName)))
		{
			return IC_FAIL_GEN;
		}

		if (ICEFAIL(FindSource(szName,pBuf)))
		{
			return IC_FAIL_GEN;
		}
	}


    icSoundAL* alSound = (icSoundAL*)sound;

    alSound->m_buf = pBuf;

    // assign the buffer to this source
    alSourcei(alSound->m_ALsource, AL_BUFFER, pBuf.buffID);

    ALenum err = alGetError();
    if (err != AL_NO_ERROR)
        return IC_FAIL_GEN;

	*ppSound = sound;

    return IC_OK;
}// END FUNCTION GetSound(const char* szName, icSoundI** ppSound)
Ejemplo n.º 7
0
void StepWorldDistributed(world_t &world, float dt, unsigned n)
{
    distCL::distributedCL distributedCL(NULL, NULL);
    int devType = CL_DEVICE_TYPE_CPU;

    cl_int err;
    // Get platforms
    cl_platform_id cpPlatform; // OpenCL platform
    cl_device_id device_id;    // compute device id
    cl_context context;        // compute context
    cl_command_queue commands; // compute command queue
    cl_program program;        // compute program
    cl_kernel kernel;          // compute kernel

    err = distributedCL.GetPlatformIDs(1, &cpPlatform, NULL, {});
    err = distributedCL.GetDeviceIDs(cpPlatform, devType, 1, &device_id, NULL, {});
    err = distributedCL.CreateContext(&context, 0, 1, &device_id, NULL, NULL, {});
    err = distributedCL.CreateCommandQueue(&commands, context, device_id, 0, {});

    // Load kernel to string
    std::string kernelSource = LoadSource("step_world_kernel.cl");
    const char *src = kernelSource.c_str();

    err = distributedCL.CreateProgramWithSource(&program, context, 1,
                                                &src,
                                                NULL, {});

    err = distributedCL.BuildProgram(program, 0, NULL, NULL, NULL, NULL, {});

    cl_mem buffProperties, buffState, buffBuffer;

    distributedCL.BroadcastValue(1, &world.w, 0);
    distributedCL.BroadcastValue(1, &world.h, 0);
    distributedCL.BroadcastValue(1, &world.alpha, 0);

    size_t split_world = world.w * (world.h / 2) + world.w;
    size_t world_size = world.w * world.h;

    data_barrier<uint32_t> properties = distributedCL.CreateBarrier<uint32_t>(world_size, world.w, context, { 0, 1 });
    if (distributedCL.world_rank == 0)
    {
        std::copy(&world.properties[0], &world.properties[0] + world_size, properties.data);
    }

    data_barrier<float> state = distributedCL.CreateBarrier<float>(world_size, world.w, context, { 0, 1 });
    if (distributedCL.world_rank == 0)
    {
        std::copy(&world.state[0], &world.state[0] + world_size, state.data);
    }

    err = distributedCL.CreateBuffer(&buffProperties, context, CL_MEM_READ_ONLY,
                                     split_world * sizeof(uint32_t), NULL, {});
    err = distributedCL.CreateBuffer(&buffState, context, CL_MEM_READ_WRITE,
                                     split_world * sizeof(float), NULL, {});
    err = distributedCL.CreateBuffer(&buffBuffer, context, CL_MEM_READ_WRITE,
                                     split_world * sizeof(float), NULL, {});

    if (!buffProperties || !buffState || !buffBuffer)
    {
        std::cerr << "Error: Failed to allocate device memory!" << std::endl;
        exit(1);
    }

    err = distributedCL.CreateKernel(&kernel, program, "kernel_xy", {});

    float outer = world.alpha * dt; // We spread alpha to other cells per time
    float inner = 1 - outer / 4;    // Anything that doesn't spread stays

    err = distributedCL.SetKernelArg(kernel, 0, sizeof(float),
                                     &inner, {});

    err |= distributedCL.SetKernelArg(kernel, 1, sizeof(float),
                                      &outer, {});
    err |= distributedCL.SetKernelArg(kernel, 3, sizeof(cl_mem),
                                      &buffProperties, {});

    if (err != CL_SUCCESS)
    {
        std::cerr << "Error: Failed to set kernel arguments! " << err << std::endl;
        exit(1);
    }

    size_t row = world.w;
    size_t mid_world = world.h/2;

    err = distributedCL.EnqueueWriteBuffer(commands, buffProperties, CL_TRUE,
                                           0, split_world,
                                           &properties, 0,
                                           0, NULL,
                                           NULL, NULL,
                                           0, { 0 });

    err = distributedCL.EnqueueWriteBuffer(commands, buffState, CL_TRUE,
                                           0, split_world,
                                           &state, 0,
                                           0, NULL,
                                           NULL, NULL,
                                           0, { 0 });

    err = distributedCL.EnqueueWriteBuffer(commands, buffProperties, CL_TRUE,
                                           0, split_world,
                                           &properties, row * (mid_world-1),
                                           0, NULL,
                                           NULL, NULL,
                                           0, { 1 });

    err = distributedCL.EnqueueWriteBuffer(commands, buffState, CL_TRUE,
                                           0, split_world,
                                           &state, row * (mid_world-1),
                                           0, NULL,
                                           NULL, NULL,
                                           0, { 1 });

    size_t *offset_0 = new size_t[2];
    offset_0[0] = 0;
    offset_0[1] = 0;

    size_t *offset_1 = new size_t[2];
    offset_1[0] = 0;
    offset_1[1] = 1;

    // size_t *global = new size_t[2];
    // global[0] = world.w;
    // global[1] = world.h;
    // global[1] = world.h/2+1;

    size_t *global = new size_t[2];
    global[0] = world.w;
    global[1] =mid_world;

    for (unsigned t = 0; t < n; ++t)
    {
        err = distributedCL.SetKernelArg(kernel, 2, sizeof(cl_mem),
                                         &buffState, {});
        err = distributedCL.SetKernelArg(kernel, 4, sizeof(cl_mem),
                                         &buffBuffer, {});


        err = distributedCL.EnqueueReadBuffer(commands, buffState, CL_TRUE,
                                              row * (mid_world-1), row,
                                              &state, row * (mid_world-1),
                                              0, NULL,
                                              NULL, NULL,
                                              0, { 1 });

        err = distributedCL.EnqueueWriteBuffer(commands, buffState, CL_TRUE,
                                               0, row,
                                               &state, row * (mid_world-1),
                                               0, NULL,
                                               NULL, NULL,
                                               1, { 1 });

        err = distributedCL.EnqueueReadBuffer(commands, buffState, CL_TRUE,
                                              row, row,
                                              &state, row * (mid_world),
                                              0, NULL,
                                              NULL, NULL,
                                              1, { 0 });

        err = distributedCL.EnqueueWriteBuffer(commands, buffState, CL_TRUE,
                                               row * (mid_world), row,
                                               &state, row * (mid_world),
                                               0, NULL,
                                               NULL, NULL,
                                               0, { 0 });

        err = distributedCL.EnqueueNDRangeKernel(commands, kernel,
                                                 2,
                                                 offset_0,
                                                 global,
                                                 NULL,
                                                 0,
                                                 NULL,
                                                 NULL, { 0 });

        err = distributedCL.EnqueueNDRangeKernel(commands, kernel,
                                                 2,
                                                 offset_1,
                                                 global,
                                                 NULL,
                                                 0,
                                                 NULL,
                                                 NULL, { 1 });

        err = distributedCL.EnqueueBarrier(commands, {});


        // rows 0-5, 4-9
        // machine 0: read buffer row 4 into state 4
        // machine 1: read buffer row 1 into state 5
        // machine 0: read state 5 into row 5
        // machine 1: read state 4 into row 0

        std::swap(buffState, buffBuffer);
        world.t += dt;
    }

    delete[] global;
    delete[] offset_0;
    delete[] offset_1;

    // err = clEnqueueReadBuffer(commands, buffState, CL_TRUE, 0, split_world, &world.state[0], 0, NULL, NULL);
    err = distributedCL.EnqueueReadBuffer(commands, buffState, CL_TRUE, 0,
                                          split_world, &state, row * (mid_world-1), 0, NULL,
                                          NULL, NULL,
                                          1, { 0 });

    err = distributedCL.EnqueueReadBuffer(commands, buffState, CL_TRUE, 0,
                                          split_world-row, &state, 0, 0, NULL,
                                          NULL, NULL,
                                          0, { 0 });

 

    if (distributedCL.world_rank == 0)
    {
        std::copy(state.data, state.data + world_size, &world.state[0]);
    }
    clReleaseMemObject(buffProperties);
    clReleaseMemObject(buffState);
    clReleaseMemObject(buffBuffer);
    clReleaseProgram(program);
    clReleaseKernel(kernel);
    clReleaseCommandQueue(commands);
    clReleaseContext(context);

    distributedCL.Finalize();
}
Ejemplo n.º 8
0
XhtmlDoc::WellFormedError ImportHTML::CheckValidToLoad()
{
    // For HTML & XML documents we will perform a well-formed check
    return XhtmlDoc::WellFormedErrorForSource(LoadSource());
}
Ejemplo n.º 9
0
 bool CShader::Compile(cb::string const & source) {
   LoadSource(source);
   return Compile();
 }
Ejemplo n.º 10
0
void StepWorldV4DoubleBuffered(world_t &world, float dt, unsigned n)
{
    distCL::distributedCL distributedCL(NULL, NULL);
    int devType = CL_DEVICE_TYPE_GPU;

    cl_int err;
    // Get platforms
    cl_platform_id cpPlatform; // OpenCL platform
    cl_device_id device_id;    // compute device id
    cl_context context;        // compute context
    cl_command_queue commands; // compute command queue
    cl_program program;        // compute program
    cl_kernel kernel;          // compute kernel

    err = distributedCL.GetPlatformIDs(1, &cpPlatform, NULL, {});
    err = distributedCL.GetDeviceIDs(cpPlatform, devType, 1, &device_id, NULL, {});
    err = distributedCL.CreateContext(&context, 0, 1, &device_id, NULL, NULL, {});
    err = distributedCL.CreateCommandQueue(&commands, context, device_id, 0, {});

    // Load kernel to string
    std::string kernelSource = LoadSource("step_world_v3_kernel.cl");
    const char *src = kernelSource.c_str();

    err = distributedCL.CreateProgramWithSource(&program, context, 1,
                                                &src,
                                                NULL, {});

    err = distributedCL.BuildProgram(program, 0, NULL, NULL, NULL, NULL, {});

    cl_mem buffProperties, buffState, buffBuffer;

    sync_data(&world.w, 0);
    sync_data(&world.h, 0);
    sync_data(&world.alpha, 0);

    fprintf(stderr, "%d %d %f\n", world.w, world.h, world.alpha);

    size_t world_size = world.w * world.h;

    data_barrier<uint32_t> properties = distributedCL.CreateBarrier<uint32_t>(world_size, world.w, context, { 0, 1 });
    if (distributedCL.world_rank == 0)
    {
        std::copy(&world.properties[0], &world.properties[0] + world_size, properties.data);
    }

    

    data_barrier<float> state = distributedCL.CreateBarrier<float>(world_size, world.w, context, { 0, 1 });
    if (distributedCL.world_rank == 0)
    {
        std::copy(&world.state[0], &world.state[0] + world_size, state.data);
    }

    err = distributedCL.CreateBuffer(&buffProperties, context, CL_MEM_READ_ONLY,
                                     world_size * sizeof(uint32_t), NULL, {});
    err = distributedCL.CreateBuffer(&buffState, context, CL_MEM_READ_WRITE,
                                     world_size * sizeof(float), NULL, {});
    err = distributedCL.CreateBuffer(&buffBuffer, context, CL_MEM_READ_WRITE,
                                     world_size * sizeof(float), NULL, {});

    if (!buffProperties || !buffState || !buffBuffer)
    {
        std::cerr << "Error: Failed to allocate device memory!" << std::endl;
        exit(1);
    }

    err = distributedCL.CreateKernel(&kernel, program, "kernel_xy", {});

    float outer = world.alpha * dt; // We spread alpha to other cells per time
    float inner = 1 - outer / 4;    // Anything that doesn't spread stays

    err = distributedCL.SetKernelArg(kernel, 0, sizeof(float),
                                     &inner, {});

    err |= distributedCL.SetKernelArg(kernel, 1, sizeof(float),
                                      &outer, {});
    err |= distributedCL.SetKernelArg(kernel, 3, sizeof(cl_mem),
                                      &buffProperties, {});

    if (err != CL_SUCCESS)
    {
        std::cerr << "Error: Failed to set kernel arguments! " << err << std::endl;
        exit(1);
    }

    err = distributedCL.EnqueueWriteBuffer(commands, buffProperties, CL_TRUE,
                                           0, world_size,
                                           &properties, 0,
                                           0, NULL,
                                           NULL, NULL,
                                           0, { 0, 1 });

    err = distributedCL.EnqueueWriteBuffer(commands, buffState, CL_TRUE,
                                           0, world_size,
                                           &state, 0,
                                           0, NULL,
                                           NULL, NULL,
                                           0, { 0, 1 });


    size_t *offset = new size_t[2];
    offset[0] = 0;
    offset[1] = 0;

    size_t *global = new size_t[2];
    global[0] = world.w;
    global[1] = world.h;

    size_t row = world.w;

    for (unsigned t = 0; t < n; ++t)
    {
        err = distributedCL.SetKernelArg(kernel, 2, sizeof(cl_mem),
                                         &buffState, {});
        err = distributedCL.SetKernelArg(kernel, 4, sizeof(cl_mem),
                                         &buffBuffer, {});

        err = distributedCL.EnqueueNDRangeKernel(commands, kernel,
                                                 2,
                                                 offset,
                                                 global,
                                                 NULL,
                                                 0,
                                                 NULL,
                                                 NULL, {});


        //  err = distributedCL.EnqueueReadBuffer(commands, buffState, CL_TRUE,
        //                                       row * 4, row,
        //                                       &state, row * 4,
        //                                       0, NULL,
        //                                       NULL, NULL,
        //                                       0, { 1 });

        // // if(distributedCL.world_rank == 1)
        // {
        //   fprintf(stderr, "%u   ", t);
        //   for (int i = 0; i <row; ++i)
        //   {
        //     fprintf(stderr, "%f ", state.data[row*4 + i]);
        //   }
        //   fprintf(stderr, "\n");
        // }
        clEnqueueBarrier(commands);
        
        std::swap(buffState, buffBuffer);
        world.t += dt;
    }

    delete[] global;
    delete[] offset;

    // err = clEnqueueReadBuffer(commands, buffState, CL_TRUE, 0, world_size, &world.state[0], 0, NULL, NULL);
    err = distributedCL.EnqueueReadBuffer(commands, buffState, CL_TRUE, 0,
                                          world_size, &state, 0, 0, NULL,
                                          NULL, NULL,
                                          0, { 0 });



    if (distributedCL.world_rank == 0)
    {
        std::copy(state.data, state.data + world_size, &world.state[0]);
    }
        clReleaseMemObject(buffProperties);
        clReleaseMemObject(buffState);
        clReleaseMemObject(buffBuffer);
        clReleaseProgram(program);
        clReleaseKernel(kernel);
        clReleaseCommandQueue(commands);
        clReleaseContext(context);
  

    distributedCL.Finalize();
}
void StepWorldV3OpenCL(world_t &world, float dt, unsigned n)
{
  // OpenCL setup
  // platform
  std::vector<cl::Platform> platforms;

  cl::Platform::get(&platforms);
  if (platforms.size() == 0)
    throw std::runtime_error("No OpenCL platforms found.");

  std::cerr << "Found " << platforms.size() << " platforms\n";
  for (unsigned i = 0; i < platforms.size(); i++) {
    std::string vendor = platforms[i].getInfo<CL_PLATFORM_VENDOR>();
    std::cerr << "  Platform " << i << " : " << vendor << "\n";
  }

  int selectedPlatform = 0;
  if (getenv("HPCE_SELECT_PLATFORM")) {
    selectedPlatform = atoi(getenv("HPCE_SELECT_PLATFORM"));
  }
  std::cerr << "Choosing platform " << selectedPlatform << "\n";
  cl::Platform platform = platforms.at(selectedPlatform);

  // device
  std::vector<cl::Device> devices;
  platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
  if (devices.size() == 0) {
    throw std::runtime_error("No opencl devices found.\n");
  }

  std::cerr << "Found " << devices.size() << " devices\n";
  for (unsigned i = 0; i < devices.size(); i++) {
    std::string name = devices[i].getInfo<CL_DEVICE_NAME>();
    std::cerr << "  Device " << i << " : " << name << "\n";
  }

  int selectedDevice = 0;
  if (getenv("HPCE_SELECT_DEVICE")) {
    selectedDevice = atoi(getenv("HPCE_SELECT_DEVICE"));
  }
  std::cerr << "Choosing device " << selectedDevice << "\n";
  cl::Device device = devices.at(selectedDevice);

  // context
  cl::Context context(devices);

  std::string kernelSource = LoadSource("step_world_v3_kernel.cl");

  cl::Program::Sources sources;  // A vector of (data,length) pairs
  sources.push_back(
      std::make_pair(kernelSource.c_str(),
                     kernelSource.size() + 1));  // push on our single string

  cl::Program program(context, sources);
  try { program.build(devices); }
  catch (...)
  {
    for (unsigned i = 0; i < devices.size(); i++) {
      std::cerr << "Log for device " << devices[i].getInfo<CL_DEVICE_NAME>()
                << ":\n\n";
      std::cerr << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[i])
                << "\n\n";
    }
    throw;
  }

  size_t cbBuffer = 4 * world.w * world.h;
  cl::Buffer buffProperties(context, CL_MEM_READ_ONLY, cbBuffer);
  cl::Buffer buffState(context, CL_MEM_READ_ONLY, cbBuffer);
  cl::Buffer buffBuffer(context, CL_MEM_WRITE_ONLY, cbBuffer);

  cl::Kernel kernel(program, "kernel_xy");

  float outer = world.alpha * dt;  // We spread alpha to other cells per time
  float inner = 1 - outer / 4;     // Anything that doesn't spread stays

  kernel.setArg(0, inner);
  kernel.setArg(1, outer);
  kernel.setArg(2, buffState);
  kernel.setArg(3, buffProperties);
  kernel.setArg(4, buffBuffer);

  cl::CommandQueue queue(context, device);

  queue.enqueueWriteBuffer(buffProperties, CL_TRUE, 0, cbBuffer,
                           &world.properties[0]);

  unsigned w = world.w, h = world.h;
  // This is our temporary working space
  std::vector<float> buffer(w * h);

  cl::NDRange offset(0, 0);      // Always start iterations at x=0, y=0
  cl::NDRange globalSize(w, h);  // Global size must match the original loops
  cl::NDRange localSize = cl::NullRange;  // We don't care about local size

  for (unsigned t = 0; t < n; t++) {
    cl::Event evCopiedState;
    queue.enqueueWriteBuffer(buffState, CL_FALSE, 0, cbBuffer, &world.state[0],
                             NULL, &evCopiedState);

    std::vector<cl::Event> kernelDependencies(1, evCopiedState);
    cl::Event evExecutedKernel;
    queue.enqueueNDRangeKernel(kernel, offset, globalSize, localSize,
                               &kernelDependencies, &evExecutedKernel);

    std::vector<cl::Event> copyBackDependencies(1, evExecutedKernel);
    queue.enqueueReadBuffer(buffBuffer, CL_TRUE, 0, cbBuffer, &buffer[0],
                            &copyBackDependencies);

    std::swap(world.state, buffer);
    world.t += dt;  // We have moved the world forwards in time
  }
}
AFILE *makefp(DEBFILE *pdf, ULONG mid, ULONG instaddr, UCHAR *fname)
{
 uchar    *cp = NULL;
 uint      segbase;
 uchar    *srcbuf;
 uint      Nlines;
 uint      Tlines;
 SEL       srcsel;
 SEL       offsel;

 ushort    srcseglen;
 ushort   *offtab;
 AFILE    *fp;
 int       ftype;
 int       OffsetTableBufSize;
 char      FileNameBuffer[CCHMAXPATH];
 char      FileSpecBuffer[CCHMAXPATH];
 UCHAR     FileNameLength;
 LNOTAB   *pLnoTabEntry;
 int       sfi;
 MODULE   *pModule;
 CSECT    *pCsect;
 UCHAR    *pFileName;
 UCHAR    *fn;
 BOOL      found;
 ULONG     junk;
 int       lno;

 if( fname )
 {
  /***************************************************************************/
  /* - if fname is specified, then the view is to be build using the source  */
  /*   file name supplied by the user as in the case of getfile.             */
  /* - we need to find an sfi to associate with the file.                    */
  /* - build a length-prefixed z-string from the supplied name.              */
  /***************************************************************************/
  fn         = strrchr(fname, '\\');
  fn         = (fn) ? (fn + 1) : fname ;
  pFileName  = Talloc( strlen(fn) + 2 );
  *pFileName = (UCHAR)strlen(fn);
  strcpy( pFileName+1, fn );

  found = FALSE;
  for( sfi=mid=0, pdf = pnode->ExeStruct; pdf != NULL; pdf=pdf->next )
  {
   mid = MapSourceFileToMidSfi( pFileName, &sfi, pdf );
   if( mid && sfi )
   {
    found = TRUE;
    memset(FileNameBuffer, 0, sizeof(FileNameBuffer) );
    strcpy(FileNameBuffer+1, fname );
    FileNameBuffer[0] = (UCHAR)strlen(fname);
    break;
   }
  }

  Tfree( pFileName );

  if( found == FALSE )
   return(NULL);

  instaddr = DBMapLno(mid, 0, sfi, &junk , pdf );
  DBMapInstAddr(instaddr, &pLnoTabEntry, pdf);
  lno = 0;
  if( pLnoTabEntry )
   lno = pLnoTabEntry->lno;
 }
 else
 {
  /***************************************************************************/
  /* - we're going to build the view from an instruction address.            */
  /***************************************************************************/
  if ( pdf->SrcOrAsm == SOURCE_LEVEL )
  {
   mid = DBMapInstAddr(instaddr, &pLnoTabEntry, pdf);
   lno = sfi = 0;
   if( pLnoTabEntry )
   {
    lno = pLnoTabEntry->lno;
    sfi = pLnoTabEntry->sfi;
   }
   if( (pLnoTabEntry != NULL) && (sfi != 0) )
   {
    memset(FileNameBuffer, 0, sizeof(FileNameBuffer) );
    cp = GetFileName( mid, sfi );
    if( cp )
     strcpy(FileNameBuffer, cp);
   }
  }
 }

 if(ftype)
 {
  findsrc(FileNameBuffer+1,FileSpecBuffer+1,sizeof(FileSpecBuffer) );
  FileSpecBuffer[0] = (UCHAR)strlen(FileSpecBuffer+1);
  memcpy(FileNameBuffer,FileSpecBuffer,sizeof(FileSpecBuffer));
 }

 FileNameLength = FileNameBuffer[0];

 fp = (AFILE*) Talloc(SizeOfAFILE(FileNameLength));
 memcpy( fp->filename, FileNameBuffer, FileNameLength+1);

 /****************************************************************************/
 /* - allocate 64k for the source buffer.                                    */
 /* - allocate 20k for the offset buffer.                                    */
 /* - load source file into a buffer and define:                             */
 /*    - srcsel    =  source buffer selector.                                */
 /*    - offsel    =  offset buffer selector.                                */
 /*    - 0         =  number of lines to skip at the beginning of the file.  */
 /*    - srcseglen =  source buffer bytes actually used by the load.         */
 /*    - Nlines    =  number of source lines in the buffer.                  */
 /*    - Tlines    =  number of source lines in the entire file.             */
 /* - reallocate the source buffer to size really needed.                    */
 /****************************************************************************/
 Nlines = srcsel = offsel = 0;
 if( !DosAllocSeg(0,(PSEL)&srcsel,0) &&
     !DosAllocSeg(20*1024,(PSEL)&offsel, 0) )
 {
  LoadSource( fp->filename+1, (UCHAR *)Sel2Flat(srcsel),
             (USHORT *)Sel2Flat(offsel), 0, &srcseglen, &Nlines, &Tlines);
  if( Nlines )
   DosReallocSeg(srcseglen, srcsel);
 }

 /****************************************************************************/
 /* - now, define the view structure.                                        */
 /****************************************************************************/
 fp->shower   = showA;
 fp->pdf      = pdf;
 fp->mid      = mid;
 fp->sfi      = sfi;
 fp->mseg     = segbase;
 fp->Tlines   = Tlines;                 /* number of lines in file.          */
 fp->Nlines   = Nlines;                 /* number of lines in source buffer. */
 fp->Nbias    = 0;                      /* number of lines skipped in source.*/
 fp->topline  = 1;                      /* init to 1st line in the file.     */
 fp->csrline  = lno;                    /*  " "                              */
 fp->hotline  = lno;                    /*  " "                              */
 fp->hotaddr  = instaddr;
 fp->skipcols = 0;                      /* columns skipped on left.          */
 fp->Nshown   = 0;
 fp->topoff   = instaddr;
 fp->csr.row  = 0;
 fp->csr.col  = 0;
 fp->csr.mode = CSR_NORMAL;
 fp->sview    = NOSRC;                  /* assume no source disassembler view*/
 fp->flags    = ASM_VIEW_NEW;

 if( Nlines )
 {
  srcbuf = (uchar*)Sel2Flat(srcsel);
  fp->source = srcbuf;

  /**************************************************************************/
  /* Allocate the offtab[] buffer to hold Nlines + 1 offsets. We add the 1  */
  /* so that we can make the offset table indices line up with the          */
  /* source line numbers.                                                   */
  /**************************************************************************/
  OffsetTableBufSize = (Nlines+1)*sizeof(USHORT);
  offtab             = (USHORT*) Talloc(OffsetTableBufSize);

  memcpy(offtab + 1, (uchar*)Sel2Flat(offsel), Nlines*sizeof(USHORT) );

  fp->offtab = offtab;
  fp->flags  = 0;                      /* clear asm flag.                   */

  if( Tlines > Nlines )                /* does compressed source exceed 64k?*/
   fp->flags |= AF_HUGE;               /* mark afile with huge file flag    */

  fp->shower = showC;                  /*  display source                   */
  fp->sview  = MIXEDN;                 /* assume no source disassembler view*/

/*
  Flag all text lines for which a (line #, offset) pair exists.
*/

  pModule = GetModuleWithAddr( instaddr, pdf );
  if( pModule )
  {
   for(pCsect = pModule->pCsects; pCsect != NULL; pCsect=pCsect->next )
   {
    int NumEntries;

    NumEntries   = pCsect->NumEntries;
    pLnoTabEntry = pCsect->pLnoTab;

    if( (pLnoTabEntry != NULL) && ( NumEntries > 0 ) )
    {
     for( ; NumEntries; pLnoTabEntry++, NumEntries-- )
     {
      if( pLnoTabEntry->sfi == sfi )
      {
       int lno;

       lno = pLnoTabEntry->lno;
       if( (lno != 0) && (lno <= Nlines) )
       {
        *(srcbuf + offtab[lno] - 1) |= LINE_OK;
       }
      }
     }
    }
   }
  }
  MarkLineBRKs( fp );                   /* mark the active breakpoints       */
 }

 if( offsel )                           /* if there was an offset segment    */
  DosFreeSeg(offsel);                   /* allocated then free it up         */
 if( !Nlines && srcsel )                /* if there was no source then       */
  DosFreeSeg(srcsel);                   /* free up the temp source buffer    */

 return( fp );
}
void StepWorldV4DoubleBuffered(world_t &world, float dt, unsigned n)
{
    // Get platforms
    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);
    if (platforms.size() == 0)
        throw std::runtime_error("No OpenCL platforms found.");

#ifdef DEBUG
    std::cerr << "Found " << platforms.size() << " platforms\n";
    for (unsigned i = 0; i < platforms.size(); i++)
    {
        std::string vendor = platforms[0].getInfo<CL_PLATFORM_VENDOR>();
        std::cerr << "  Platform " << i << " : " << vendor << "\n";
    }
#endif

    // select platforms
    int selectedPlatform = 0;
    if (getenv("HPCE_SELECT_PLATFORM"))
    {
        selectedPlatform = atoi(getenv("HPCE_SELECT_PLATFORM"));
    }

#ifdef DEBUG
    std::cerr << "Choosing platform " << selectedPlatform << "\n";
#endif

    cl::Platform platform = platforms.at(selectedPlatform);

    // Get devices
    std::vector<cl::Device> devices;
    platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
    if (devices.size() == 0)
    {
        throw std::runtime_error("No opencl devices found.\n");
    }

#ifdef DEBUG
    std::cerr << "Found " << devices.size() << " devices\n";
    for (unsigned i = 0; i < devices.size(); i++)
    {
        std::string name = devices[i].getInfo<CL_DEVICE_NAME>();
        std::cerr << "  Device " << i << " : " << name << "\n";
    }
#endif

    // Select device
    int selectedDevice = 0;
    if (getenv("HPCE_SELECT_DEVICE"))
    {
        selectedDevice = atoi(getenv("HPCE_SELECT_DEVICE"));
    }
#ifdef DEBUG
    std::cerr << "Choosing device " << selectedDevice << "\n";
#endif

    cl::Device device = devices.at(selectedDevice);

    // Create context
    cl::Context context(devices);

    // Load kernel to string
    std::string kernelSource = LoadSource("step_world_v3_kernel.cl");

    // Load kernel to sources
    cl::Program::Sources sources;   // A vector of (data,length) pairs
    sources.push_back(std::make_pair(kernelSource.c_str(), kernelSource.size() + 1)); // push on our single string

    // Create program from context
    cl::Program program(context, sources);

#ifdef DEBUG
    try
    {
        program.build(devices);
    }
    catch (...)
    {
        for (unsigned i = 0; i < devices.size(); i++)
        {
            std::cerr << "Log for device " << devices[i].getInfo<CL_DEVICE_NAME>() << ":\n\n";
            std::cerr << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[i]) << "\n\n";
        }
        throw;
    }
#else
    program.build(devices);
#endif

    size_t cbBuffer = 4 * world.w * world.h;
    cl::Buffer buffProperties(context, CL_MEM_READ_ONLY, cbBuffer);
    cl::Buffer buffState(context, CL_MEM_READ_WRITE, cbBuffer);
    cl::Buffer buffBuffer(context, CL_MEM_READ_WRITE, cbBuffer);

    cl::Kernel kernel(program, "kernel_xy");

    unsigned w = world.w, h = world.h;

    float outer = world.alpha * dt; // We spread alpha to other cells per time
    float inner = 1 - outer / 4;        // Anything that doesn't spread stays

    kernel.setArg(0, inner);
    kernel.setArg(1, outer);

    kernel.setArg(3, buffProperties);


    cl::CommandQueue queue(context, device);

    queue.enqueueWriteBuffer(buffProperties, CL_TRUE, 0, cbBuffer, &world.properties[0]);
    queue.enqueueWriteBuffer(buffState, CL_TRUE, 0, cbBuffer, &world.state[0]);

    cl::NDRange offset(0, 0);               // Always start iterations at x=0, y=0
    cl::NDRange globalSize(w, h);   // Global size must match the original loops
    cl::NDRange localSize = cl::NullRange;  // We don't care about local size

    for (unsigned t = 0; t < n; t++)
    {
        kernel.setArg(2, buffState);
        kernel.setArg(4, buffBuffer);

        queue.enqueueNDRangeKernel(kernel, offset, globalSize, localSize);
        queue.enqueueBarrier();

        // queue.enqueueCopyBuffer(buffBuffer, buffState, 0, 0, cbBuffer, 0, NULL);
        // queue.enqueueBarrier();

        std::swap(buffState, buffBuffer);
        // queue.enqueueReadBuffer(buffBuffer, CL_TRUE, 0, cbBuffer, &buffer[0]);
        // Swapping rather than assigning is cheaper: just a pointer swap
        // rather than a memcpy, so O(1) rather than O(w*h)

        world.t += dt; // We have moved the world forwards in time

    } // end of for(t...

    // This is our temporary working space
    queue.enqueueReadBuffer(buffState, CL_TRUE, 0, cbBuffer, &world.state[0]);
}
Ejemplo n.º 14
0
std::tr1::tuple<cl::Kernel,cl::Kernel,std::vector<cl::Buffer*>,cl::CommandQueue,cl::NDRange,cl::NDRange,cl::NDRange> init_cl(int levels, unsigned w, unsigned h, unsigned bits, std::string source, int deviceNumber)
{
    std::vector<cl::Platform> platforms;
    
	cl::Platform::get(&platforms);
	
    if(platforms.size()==0) throw std::runtime_error("No OpenCL platforms found.");
    
    std::cerr<<"Found "<<platforms.size()<<" platforms\n";
	for(unsigned i=0;i<platforms.size();i++){
		std::string vendor=platforms[0].getInfo<CL_PLATFORM_VENDOR>();
		std::cerr<<"  Platform "<<i<<" : "<<vendor<<"\n";
	}
    
    int selectedPlatform=0;
	if(getenv("HPCE_SELECT_PLATFORM")){
		selectedPlatform=atoi(getenv("HPCE_SELECT_PLATFORM"));
	}
	std::cerr<<"Choosing platform "<<selectedPlatform<<"\n";
	cl::Platform platform=platforms.at(selectedPlatform);
    
    std::vector<cl::Device> devices;
	platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
	if(devices.size()==0){
		throw std::runtime_error("No opencl devices found.\n");
	}
    
	std::cerr<<"Found "<<devices.size()<<" devices\n";
	for(unsigned i=0;i<devices.size();i++){
		std::string name=devices[i].getInfo<CL_DEVICE_NAME>();
		std::cerr<<"  Device "<<i<<" : "<<name<<"\n";
	}
    
    int selectedDevice=0;
    
    if (deviceNumber != -1) selectedDevice = deviceNumber;
    
	std::cerr<<"Choosing device "<<selectedDevice<<"\n";
	cl::Device device=devices.at(selectedDevice);
    
    cl::Context context(devices);
    
    std::string kernelSource=LoadSource(source.c_str());
	
	cl::Program::Sources sources;	// A vector of (data,length) pairs
	sources.push_back(std::make_pair(kernelSource.c_str(), kernelSource.size()+1));	// push on our single string
    
	cl::Program program(context, sources);
	try{
		program.build(devices);
	}catch(...){
		for(unsigned i=0;i<devices.size();i++){
			std::cerr<<"Log for device "<<devices[i].getInfo<CL_DEVICE_NAME>()<<":\n\n";
			std::cerr<<program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[i])<<"\n\n";
		}
		throw;
	}
    
    size_t cbBuffer= (w*bits)/2;
    
    std::vector<cl::Buffer*> gpuBuffers;
    
    for (int i=0; i<abs(levels); i++)
    {
        gpuBuffers.push_back(new cl::Buffer(context, CL_MEM_READ_WRITE, cbBuffer));
        gpuBuffers.push_back(new cl::Buffer(context, CL_MEM_READ_WRITE, cbBuffer));
    }
    gpuBuffers.push_back(new cl::Buffer(context, CL_MEM_READ_WRITE, cbBuffer)); // ... and one for luck.
    
    std::string erodeKernelName;
    std::string dilateKernelName;
    
    switch (bits) {
        case 1:
            erodeKernelName = "erode_kernel_1";
            dilateKernelName = "dilate_kernel_1";
            break;
        case 2:
            erodeKernelName = "erode_kernel_2";
            dilateKernelName = "dilate_kernel_2";
            break;
        case 4:
            erodeKernelName = "erode_kernel_4";
            dilateKernelName = "dilate_kernel_4";
            break;
        case 8:
            erodeKernelName = "erode_kernel_8";
            dilateKernelName = "dilate_kernel_8";
            break;
        case 16:
            erodeKernelName = "erode_kernel_16";
            dilateKernelName = "dilate_kernel_16";
            break;
        case 32:
            erodeKernelName = "erode_kernel_32";
            dilateKernelName = "dilate_kernel_32";
            break;
        default:
            break;
    }
    
    cl::Kernel erodeKernel(program, erodeKernelName.c_str());
    cl::Kernel dilateKernel(program, dilateKernelName.c_str());
    
    cl::CommandQueue queue(context, device);
    
    cl::NDRange offset(0, 0);				 // Always start iterations at x=0, y=0
    cl::NDRange globalSize((w*bits)/32, 1);  // Global size must match the original loops
    cl::NDRange localSize=cl::NullRange;	 // We don't care about local size
    
    return std::tr1::make_tuple(erodeKernel,dilateKernel,gpuBuffers,queue,offset,globalSize,localSize);
}