<commit_before>#include "THClGeneral.h"
#include "TH.h"

#include <stdio.h>
#include "EasyCL.h"
#include <clBLAS.h>
#include "DeviceInfo.h"

//using namespace easycl;

//#include "THCTensorRandom.h"
//#include "THCBlas.h"
//#include "THCAllocator.h"

/* Size of scratch space available in global memory per each SM + stream */
#define FLOATS_PER_SCRATCH_SPACE 4
#define GLOBAL_SCRATCH_SPACE_PER_SM_STREAM (FLOATS_PER_SCRATCH_SPACE) * sizeof(float)

void THClInit(THClState* state)
{
  state->allocatedDevices = easycl::DevicesInfo::getNumGpus();
  state->clByDevice = new EasyCL *[state->allocatedDevices];
  state->scratchSpaceByDevice = new THClScratchSpace *[state->allocatedDevices];
  state->trace = 0;
  state->addFinish = 0;
//  state->workgroupSizeByDevice = new int[state->allocatedDevices];
  state->deviceInfoByDevice = (struct DeviceInfo **) new easycl::DeviceInfo *[state->allocatedDevices];
  for(int i = 0; i < state->allocatedDevices; i++) {
    state->clByDevice[i] = 0;
    state->scratchSpaceByDevice[i] = 0;
    state->deviceInfoByDevice[i] = 0;
  }
  state->currentDevice = 0;
  //state->cl = EasyCL::createForFirstGpuOtherwiseCpu(); // obviously this should change...

    cl_int err;

    err = clblasSetup();
    if (err != CL_SUCCESS) {
        THError("clblasSetup() failed with %d", err);
    }

}

void THClShutdown(THClState* state)
{
//  printf("THClShutdown() start...\n");
//  for(int i = 0; i < state->allocatedDevices; i++) {
//    delete state->clByDevice[i];
//    if( state->scratchSpaceByDevice[i] != 0 ) {
//      delete state->scratchSpaceByDevice[i]->wrapper;
//      delete state->scratchSpaceByDevice[i]->data;
//      delete state->scratchSpaceByDevice[i];
//    }
//  }
//  delete state->clByDevice;
    clblasTeardown();
  for( int i = 0; i < state->allocatedDevices; i++ ) {
    delete state->clByDevice[i];
    delete state->scratchSpaceByDevice[i]->wrapper;
    delete[] state->scratchSpaceByDevice[i]->data;
    delete (easycl::DeviceInfo*)state->deviceInfoByDevice[i];
  }
  delete[] (easycl::DeviceInfo**)state->deviceInfoByDevice;
  delete[] state->clByDevice;
  delete[] state->scratchSpaceByDevice;
//  delete[] state->workgroupSizeByDevice

  printf("THClShutdown() done\n");
  printf("*******************************************\n");
}

std::ostream &operator<<( std::ostream &os, const dim3 &obj ) {
  os << "dim3{" << obj.vec[0] << ", " << obj.vec[1] << ", " << obj.vec[2] << "}";
  return os;
}

int THClState_getNumDevices(THClState* state) {
  return state->allocatedDevices;
}
void THClState_setDevice(THClState* state, int device) {
  state->currentDevice = device;
}
int THClState_getDevice(THClState* state) {
  return state->currentDevice;
}
EasyCL *THClState_getCl(THClState* state) {
  return THClState_getClAndDevice(state, 0);
}
EasyCL *THClState_getClAndDevice(THClState* state, int *p_device) {
  int device = state->currentDevice;
  if(state->allocatedDevices == 0) {
    THError("No OpenCL-enabled devices available");
  }
  if(state->currentDevice >= state->allocatedDevices || state->currentDevice < 0) {
    THError("Please use setDevice to choose an available device first");
  }
  if( state->clByDevice[device] == 0 ) {
    EasyCL *cl = EasyCL::createForIndexedGpu(device);
    state->clByDevice[device] = cl;
    THClScratchSpace *scratch = new THClScratchSpace();
    scratch->data = new float[FLOATS_PER_SCRATCH_SPACE];
    scratch->wrapper = cl->wrap(FLOATS_PER_SCRATCH_SPACE, scratch->data);
    scratch->wrapper->createOnDevice();
    state->scratchSpaceByDevice[device] = scratch;
    state->deviceInfoByDevice[device] = (struct DeviceInfo *)new easycl::DeviceInfo();
    *((easycl::DeviceInfo *)state->deviceInfoByDevice[device]) = easycl::DevicesInfo::getDeviceInfo( device );
  }
  if( p_device != 0 ) {<commit_msg>fix discrepancy between gpus and cpus<commit_after>#include "THClGeneral.h"
#include "TH.h"

#include <stdio.h>
#include "EasyCL.h"
#include <clBLAS.h>
#include "DeviceInfo.h"

//using namespace easycl;

//#include "THCTensorRandom.h"
//#include "THCBlas.h"
//#include "THCAllocator.h"

/* Size of scratch space available in global memory per each SM + stream */
#define FLOATS_PER_SCRATCH_SPACE 4
#define GLOBAL_SCRATCH_SPACE_PER_SM_STREAM (FLOATS_PER_SCRATCH_SPACE) * sizeof(float)

void THClInit(THClState* state)
{
  state->allocatedDevices = easycl::DevicesInfo::getNumGpus();
  state->clByDevice = new EasyCL *[state->allocatedDevices];
  state->scratchSpaceByDevice = new THClScratchSpace *[state->allocatedDevices];
  state->trace = 0;
  state->addFinish = 0;
//  state->workgroupSizeByDevice = new int[state->allocatedDevices];
  state->deviceInfoByDevice = (struct DeviceInfo **) new easycl::DeviceInfo *[state->allocatedDevices];
  for(int i = 0; i < state->allocatedDevices; i++) {
    state->clByDevice[i] = 0;
    state->scratchSpaceByDevice[i] = 0;
    state->deviceInfoByDevice[i] = 0;
  }
  state->currentDevice = 0;
  //state->cl = EasyCL::createForFirstGpuOtherwiseCpu(); // obviously this should change...

    cl_int err;

    err = clblasSetup();
    if (err != CL_SUCCESS) {
        THError("clblasSetup() failed with %d", err);
    }

}

void THClShutdown(THClState* state)
{
//  printf("THClShutdown() start...\n");
//  for(int i = 0; i < state->allocatedDevices; i++) {
//    delete state->clByDevice[i];
//    if( state->scratchSpaceByDevice[i] != 0 ) {
//      delete state->scratchSpaceByDevice[i]->wrapper;
//      delete state->scratchSpaceByDevice[i]->data;
//      delete state->scratchSpaceByDevice[i];
//    }
//  }
//  delete state->clByDevice;
    clblasTeardown();
  for( int i = 0; i < state->allocatedDevices; i++ ) {
    delete state->clByDevice[i];
    delete state->scratchSpaceByDevice[i]->wrapper;
    delete[] state->scratchSpaceByDevice[i]->data;
    delete (easycl::DeviceInfo*)state->deviceInfoByDevice[i];
  }
  delete[] (easycl::DeviceInfo**)state->deviceInfoByDevice;
  delete[] state->clByDevice;
  delete[] state->scratchSpaceByDevice;
//  delete[] state->workgroupSizeByDevice

  printf("THClShutdown() done\n");
  printf("*******************************************\n");
}

std::ostream &operator<<( std::ostream &os, const dim3 &obj ) {
  os << "dim3{" << obj.vec[0] << ", " << obj.vec[1] << ", " << obj.vec[2] << "}";
  return os;
}

int THClState_getNumDevices(THClState* state) {
  return state->allocatedDevices;
}
void THClState_setDevice(THClState* state, int device) {
  state->currentDevice = device;
}
int THClState_getDevice(THClState* state) {
  return state->currentDevice;
}
EasyCL *THClState_getCl(THClState* state) {
  return THClState_getClAndDevice(state, 0);
}
EasyCL *THClState_getClAndDevice(THClState* state, int *p_device) {
  int device = state->currentDevice;
  if(state->allocatedDevices == 0) {
    THError("No OpenCL-enabled devices available");
  }
  if(state->currentDevice >= state->allocatedDevices || state->currentDevice < 0) {
    THError("Please use setDevice to choose an available device first");
  }
  if( state->clByDevice[device] == 0 ) {
    EasyCL *cl = EasyCL::createForIndexedGpu(device);
    state->clByDevice[device] = cl;
    THClScratchSpace *scratch = new THClScratchSpace();
    scratch->data = new float[FLOATS_PER_SCRATCH_SPACE];
    scratch->wrapper = cl->wrap(FLOATS_PER_SCRATCH_SPACE, scratch->data);
    scratch->wrapper->createOnDevice();
    state->scratchSpaceByDevice[device] = scratch;
    state->deviceInfoByDevice[device] = (struct DeviceInfo *)new easycl::DeviceInfo();
    *((easycl::DeviceInfo *)state->deviceInfoByDevice[device]) = easycl::DevicesInfo::getGpuInfo( device );
  }
  if( p_device != 0 ) {
{'fraction_non_alphanumeric': 0.16721268510024898, 'fraction_numerical': 0.005503865810509763, 'mean_word_length': 4.670133729569094, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4519463', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2622, 'n_words': 497}