<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * The Original Contributor of this Source Code Form is Nokia Research
 * Center Tampere (http://webcl.nokiaresearch.com).
 */

#include "FakeCL.h"
#include <cstring>
#include <cassert>
#include <pthread.h>
#include <cstdio>
#include <sys/types.h>

#include <map>
#include <string>
#include <vector>

// used for returning values from parameter query functions
#define R(type, value) if (param_value_size_ret) *param_value_size_ret = sizeof(type); if (sizeof(type) <= param_value_size) * (type*) param_value = value; break
#define RS(str) if (param_value_size_ret) *param_value_size_ret = sizeof(str); if (sizeof(str) <= param_value_size) std::memcpy(param_value, str, sizeof(str)); break;

const int work_group_size = 64;

namespace {
  std::map<std::string, fakecl_kernel_fn> fakecl_kernel_funcs;
}

pthread_mutex_t barrier_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t barrier_cond = PTHREAD_COND_INITIALIZER;
int barrier_max = work_group_size;
int barrier_enter = 0;          // number of threads waiting in barrier, in range 0..barrier_max
int barrier_completed_seq = 0;

typedef struct {
  void* ptr;
  size_t size;
  bool do_delete;
} cl_mem_info;

struct cl_arg {
  bool is_local;
  std::vector<char> data;
};

struct cl_kernel_struct {
  fakecl_kernel_fn fn;
  int arg_count;
  cl_arg args[FAKECL_MAX_ARGS];
};

namespace {
  std::map<cl_mem, cl_mem_info> cl_mem_data;
}

extern "C" {
void fakeclSetKernelFunc(const char* label, fakecl_kernel_fn fn)
{
  fakecl_kernel_funcs[label] = fn;
}

cl_context clCreateContext(cl_context_properties *properties,
                           cl_uint num_devices,
                           const cl_device_id *devices,
                           void *pfn_notify (
                                             const char *errinfo, 
                                             const void *private_info, 
                                             size_t cb, 
                                             void *user_data
                                             ),
                           void *user_data,
                           cl_int *errcode_ret)
{
  if (errcode_ret) {
    *errcode_ret = CL_SUCCESS;
  }
  return 0;
}

cl_context clCreateContextFromType(cl_context_properties   *properties,
                                   cl_device_type  device_type,
                                   void  (*pfn_notify) (const char *errinfo,
                                                        const void  *private_info,
                                                        size_t  cb,
                                                        void  *user_data),
                                   void  *user_data,
                                   cl_int  *errcode_ret)
{
  if (errcode_ret) {
    *errcode_ret = CL_SUCCESS;
  }
  return 0;
}

cl_int clGetContextInfo(cl_context context,
                        cl_context_info param_name,
                        size_t param_value_size,
                        void *param_value,
                        size_t* param_value_size_ret)
{
  switch (param_name) {
  case CL_CONTEXT_REFERENCE_COUNT: R(cl_uint, 1);
  case CL_CONTEXT_DEVICES:         R(cl_uint, 0);
  case CL_CONTEXT_PROPERTIES:      assert(false);
  }
  assert(false);
}

cl_command_queue clCreateCommandQueue(cl_context, cl_device_id, int, int* ret)
{
  if (ret) {
    *ret = CL_SUCCESS;<commit_msg>FakeCL: context is now '1' not '0'<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * The Original Contributor of this Source Code Form is Nokia Research
 * Center Tampere (http://webcl.nokiaresearch.com).
 */

#include "FakeCL.h"
#include <cstring>
#include <cassert>
#include <pthread.h>
#include <cstdio>
#include <sys/types.h>

#include <map>
#include <string>
#include <vector>

// used for returning values from parameter query functions
#define R(type, value) if (param_value_size_ret) *param_value_size_ret = sizeof(type); if (sizeof(type) <= param_value_size) * (type*) param_value = value; break
#define RS(str) if (param_value_size_ret) *param_value_size_ret = sizeof(str); if (sizeof(str) <= param_value_size) std::memcpy(param_value, str, sizeof(str)); break;

const int work_group_size = 64;

namespace {
  std::map<std::string, fakecl_kernel_fn> fakecl_kernel_funcs;
}

pthread_mutex_t barrier_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t barrier_cond = PTHREAD_COND_INITIALIZER;
int barrier_max = work_group_size;
int barrier_enter = 0;          // number of threads waiting in barrier, in range 0..barrier_max
int barrier_completed_seq = 0;

typedef struct {
  void* ptr;
  size_t size;
  bool do_delete;
} cl_mem_info;

struct cl_arg {
  bool is_local;
  std::vector<char> data;
};

struct cl_kernel_struct {
  fakecl_kernel_fn fn;
  int arg_count;
  cl_arg args[FAKECL_MAX_ARGS];
};

namespace {
  std::map<cl_mem, cl_mem_info> cl_mem_data;
}

extern "C" {
void fakeclSetKernelFunc(const char* label, fakecl_kernel_fn fn)
{
  fakecl_kernel_funcs[label] = fn;
}

cl_context clCreateContext(cl_context_properties *properties,
                           cl_uint num_devices,
                           const cl_device_id *devices,
                           void *pfn_notify (
                                             const char *errinfo, 
                                             const void *private_info, 
                                             size_t cb, 
                                             void *user_data
                                             ),
                           void *user_data,
                           cl_int *errcode_ret)
{
  if (errcode_ret) {
    *errcode_ret = CL_SUCCESS;
  }
  return 1;
}

cl_context clCreateContextFromType(cl_context_properties   *properties,
                                   cl_device_type  device_type,
                                   void  (*pfn_notify) (const char *errinfo,
                                                        const void  *private_info,
                                                        size_t  cb,
                                                        void  *user_data),
                                   void  *user_data,
                                   cl_int  *errcode_ret)
{
  if (errcode_ret) {
    *errcode_ret = CL_SUCCESS;
  }
  return 1;
}

cl_int clGetContextInfo(cl_context context,
                        cl_context_info param_name,
                        size_t param_value_size,
                        void *param_value,
                        size_t* param_value_size_ret)
{
  switch (param_name) {
  case CL_CONTEXT_REFERENCE_COUNT: R(cl_uint, 1);
  case CL_CONTEXT_DEVICES:         R(cl_uint, 0);
  case CL_CONTEXT_PROPERTIES:      assert(false);
  }
  assert(false);
}

cl_command_queue clCreateCommandQueue(cl_context, cl_device_id, int, int* ret)
{
  if (ret) {
    *ret = CL_SUCCESS;
{'fraction_non_alphanumeric': 0.08184461985874533, 'fraction_numerical': 0.0038775792826478326, 'mean_word_length': 1.9417515274949084, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '62293', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2133, 'n_words': 531}