<commit_before>/*
 *  cl++.cpp
 *
 *  Created by eholk on 12/3/10.
 *
 */

#include "cl++.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <stdlib.h>
#include <unistd.h>

extern cl::device_list g_devices;

using namespace std;
using namespace cl;

// in builtin.cpp
uint64_t nanotime();

std::string device::name()
{
  char n[256] = {0};
	clGetDeviceInfo(id, CL_DEVICE_NAME, sizeof(n), n, NULL);

	return std::string(n);
}

cl_device_type device::type()
{
	cl_device_type t;
	clGetDeviceInfo(id, CL_DEVICE_TYPE, sizeof(t), &t, NULL);
	return t;
}

device::operator cl_device_id() const
{
	return id;
}

device_list::device_list(cl_device_type type)
: type(type), num_ids(0), devices(NULL)
{
    cl_int status = 0;

    // Find the platforms.
    cl_platform_id *platforms;
    cl_uint nPlatforms = 0;
    CL_CHECK(clGetPlatformIDs(0, NULL, &nPlatforms));
    assert(nPlatforms > 0);

    platforms = new cl_platform_id[nPlatforms];
    CL_CHECK(clGetPlatformIDs(nPlatforms, platforms, &nPlatforms));

    // Find out how many devices there are.
    cl_uint n_dev = 0;
    for(int i = 0; i < nPlatforms; ++i) {
        status = clGetDeviceIDs(platforms[i], type, 0, NULL, &n_dev);
        if(CL_DEVICE_NOT_FOUND == status)
            continue;
        CL_CHECK_MSG(status, "clGetDeviceIDs (count)");
        num_ids += n_dev;

        char n[256] = {0};
        clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, sizeof(n), n, NULL);
        
        cerr << "OpenCL Platform Name:    " << n << endl;
        
        clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(n), n, NULL);
        
        cerr << "OpenCL Platform Vendor:  " << n << endl;

        clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, sizeof(n), n, NULL);
        
        cerr << "OpenCL Platform Version: " << n << endl;

        break;
    }

    cerr << "found " << num_ids << " devices" << endl;

    // We seem to get errors by assigning a program to too many
    // devices, so to prevent these, we arbitrarily restrict ourself
    // to one device here.
    num_ids = 1;
  
    // Allocate memory, gather information about all the devices.
    devices = new cl_device_id[num_ids];
  
    size_t offset = 0;
    for(int i = 0; i < nPlatforms; ++i) {
        status = clGetDeviceIDs(platforms[i], type, n_dev,
                                devices + offset, &n_dev);
        if(CL_DEVICE_NOT_FOUND == status)
            continue;
        CL_CHECK_MSG(status, "clGetDeviceIDs");
        offset += n_dev;
    }

    delete [] platforms;
}

device_list::~device_list()
{
	if(devices)
		delete [] devices;
}

int device_list::count() const
{
	return num_ids;
}

const cl_device_id *device_list::ids() const
{
	return devices;
}

device device_list::operator[](int index)<commit_msg>Be more robust in the face of multiple OpenCL platforms.<commit_after>/*
 *  cl++.cpp
 *
 *  Created by eholk on 12/3/10.
 *
 */

#include "cl++.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <stdlib.h>
#include <unistd.h>

extern cl::device_list g_devices;

using namespace std;
using namespace cl;

// in builtin.cpp
uint64_t nanotime();

std::string device::name()
{
  char n[256] = {0};
	clGetDeviceInfo(id, CL_DEVICE_NAME, sizeof(n), n, NULL);

	return std::string(n);
}

cl_device_type device::type()
{
	cl_device_type t;
	clGetDeviceInfo(id, CL_DEVICE_TYPE, sizeof(t), &t, NULL);
	return t;
}

device::operator cl_device_id() const
{
	return id;
}

device_list::device_list(cl_device_type type)
: type(type), num_ids(0), devices(NULL)
{
    cl_int status = 0;

    // Find the platforms.
    cl_platform_id *platforms;
    cl_uint nPlatforms = 0;
    CL_CHECK(clGetPlatformIDs(0, NULL, &nPlatforms));
    assert(nPlatforms > 0);

    //cerr << "found " << nPlatforms << " platforms." << endl;

    platforms = new cl_platform_id[nPlatforms];
    CL_CHECK(clGetPlatformIDs(nPlatforms, platforms, &nPlatforms));

    // Find out how many devices there are.
    cl_uint n_dev = 0;
    int platform = -1;
    for(int i = 0; i < nPlatforms; ++i) {
        status = clGetDeviceIDs(platforms[i], type, 0, NULL, &n_dev);
        if(CL_DEVICE_NOT_FOUND == status)
            continue;
        CL_CHECK_MSG(status, "clGetDeviceIDs (count)");
        num_ids += n_dev;

        char n[256] = {0};
        clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, sizeof(n), n, NULL);
        
        cerr << "OpenCL Platform Name:    " << n << endl;
        
        clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(n), n, NULL);
        
        cerr << "OpenCL Platform Vendor:  " << n << endl;

        clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, sizeof(n), n, NULL);
        
        cerr << "OpenCL Platform Version: " << n << endl;

        platform = i;

        break;
    }

    cerr << "found " << num_ids << " devices" << endl;

    // We seem to get errors by assigning a program to too many
    // devices, so to prevent these, we arbitrarily restrict ourself
    // to one device here.
    num_ids = 1;
  
    // Allocate memory, gather information about all the devices.
    devices = new cl_device_id[num_ids];
  
    status = clGetDeviceIDs(platforms[platform], type, n_dev,
                            devices, &n_dev);
    //cerr << status << ", " << n_dev << endl;
    CL_CHECK_MSG(status, "clGetDeviceIDs");

    delete [] platforms;
}

device_list::~device_list()
{
	if(devices)
		delete [] devices;
}

int device_list::count() const
{
	return num_ids;
}

const cl_device_id *device_list::ids() const
{
	return devices;
}

device device_list::operator[](int index)
{'fraction_non_alphanumeric': 0.13458110516934046, 'fraction_numerical': 0.00909090909090909, 'mean_word_length': 3.013590844062947, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 15, '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': '2581700', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1948, 'n_words': 487}