<commit_before>/******************************************************************************\
 *           ___        __                                                    *
 *          /\_ \    __/\ \                                                   *
 *          \//\ \  /\_\ \ \____    ___   _____   _____      __               *
 *            \ \ \ \/\ \ \ '__`\  /'___\/\ '__`\/\ '__`\  /'__`\             *
 *             \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_           *
 *             /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\          *
 *             \/____/ \/_/\/___/  \/____/ \ \ \/  \ \ \/  \/__/\/_/          *
 *                                          \ \_\   \ \_\                     *
 *                                           \/_/    \/_/                     *
 *                                                                            *
 * Copyright (C) 2011-2013                                                    *
 * <NAME> <<EMAIL>>                     *
 * <NAME> <<EMAIL>>                           *
 *                                                                            *
 * This file is part of libcppa.                                              *
 * libcppa is free software: you can redistribute it and/or modify it under   *
 * the terms of the GNU Lesser General Public License as published by the     *
 * Free Software Foundation, either version 3 of the License                  *
 * or (at your option) any later version.                                     *
 *                                                                            *
 * libcppa is distributed in the hope that it will be useful,                 *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                       *
 * See the GNU Lesser General Public License for more details.                *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with libcppa. If not, see <http://www.gnu.org/licenses/>.            *
\******************************************************************************/

#include "cppa/opencl/opencl_metainfo.hpp"

using namespace std;

namespace cppa { namespace opencl {

const std::vector<device_info> opencl_metainfo::get_devices() const {
    return m_devices;
}

void opencl_metainfo::initialize()
{
    cl_int err{0};


    // get number of available platforms
    cl_uint number_of_platforms;
    err = clGetPlatformIDs(0, nullptr, &number_of_platforms);
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clGetPlatformIDs (getting number of platforms): "
            << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw logic_error(oss.str());
    }


    // get platform ids
    vector<cl_platform_id> ids(number_of_platforms);
    err = clGetPlatformIDs(ids.size(), ids.data(), nullptr);
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clGetPlatformIDs (getting platform ids): "
            << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw logic_error(oss.str());
    }


    // find gpu devices on our platform
    int pid{0};
    cl_uint num_devices{0};
    cl_device_type dev_type{CL_DEVICE_TYPE_GPU};
    err = clGetDeviceIDs(ids[pid], dev_type, 0, nullptr, &num_devices);
    if (err == CL_DEVICE_NOT_FOUND) {
        CPPA_LOG_TRACE("No gpu devices found. Looking for cpu devices.");
        cout << "No gpu devices found. Looking for cpu devices." << endl;
        dev_type = CL_DEVICE_TYPE_CPU;
        err = clGetDeviceIDs(ids[pid], dev_type, 0, nullptr, &num_devices);
    }
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clGetDeviceIDs: " << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw runtime_error(oss.str());
    }
    vector<cl_device_id> devices(num_devices);
    err = clGetDeviceIDs(ids[pid], dev_type, num_devices, devices.data(), nullptr);
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clGetDeviceIDs: " << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw runtime_error(oss.str());
    }


    // create a context
    m_context.adopt(clCreateContext(0, 1, devices.data(), nullptr, nullptr, &err));
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clCreateContext: " << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw runtime_error(oss.str());
    }


    for (auto& d : devices) {
        CPPA_LOG_TRACE("Creating command queue for device(s).");
        device_ptr device;
        device.adopt(d);
        size_t return_size{0};
        static constexpr size_t buf_size = 128;
        char buf[buf_size];
        err = clGetDeviceInfo(device.get(), CL_DEVICE_NAME, buf_size, buf, &return_size);
        if (err != CL_SUCCESS) {
            CPPA_LOGMF(CPPA_ERROR, self, "clGetDeviceInfo (CL_DEVICE_NAME): "
                                         << get_opencl_error(err));<commit_msg>the OpenCL context is now created for all devices<commit_after>/******************************************************************************\
 *           ___        __                                                    *
 *          /\_ \    __/\ \                                                   *
 *          \//\ \  /\_\ \ \____    ___   _____   _____      __               *
 *            \ \ \ \/\ \ \ '__`\  /'___\/\ '__`\/\ '__`\  /'__`\             *
 *             \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_           *
 *             /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\          *
 *             \/____/ \/_/\/___/  \/____/ \ \ \/  \ \ \/  \/__/\/_/          *
 *                                          \ \_\   \ \_\                     *
 *                                           \/_/    \/_/                     *
 *                                                                            *
 * Copyright (C) 2011-2013                                                    *
 * <NAME> <<EMAIL>>                     *
 * <NAME> <<EMAIL>>                           *
 *                                                                            *
 * This file is part of libcppa.                                              *
 * libcppa is free software: you can redistribute it and/or modify it under   *
 * the terms of the GNU Lesser General Public License as published by the     *
 * Free Software Foundation, either version 3 of the License                  *
 * or (at your option) any later version.                                     *
 *                                                                            *
 * libcppa is distributed in the hope that it will be useful,                 *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                       *
 * See the GNU Lesser General Public License for more details.                *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with libcppa. If not, see <http://www.gnu.org/licenses/>.            *
\******************************************************************************/

#include "cppa/opencl/opencl_metainfo.hpp"

using namespace std;

namespace cppa { namespace opencl {

const std::vector<device_info> opencl_metainfo::get_devices() const {
    return m_devices;
}

void opencl_metainfo::initialize()
{
    cl_int err{0};


    // get number of available platforms
    cl_uint number_of_platforms;
    err = clGetPlatformIDs(0, nullptr, &number_of_platforms);
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clGetPlatformIDs (getting number of platforms): "
            << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw logic_error(oss.str());
    }


    // get platform ids
    vector<cl_platform_id> ids(number_of_platforms);
    err = clGetPlatformIDs(ids.size(), ids.data(), nullptr);
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clGetPlatformIDs (getting platform ids): "
            << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw logic_error(oss.str());
    }


    // find gpu devices on our platform
    int pid{0};
    cl_uint num_devices{0};
    cl_device_type dev_type{CL_DEVICE_TYPE_GPU};
    err = clGetDeviceIDs(ids[pid], dev_type, 0, nullptr, &num_devices);
    if (err == CL_DEVICE_NOT_FOUND) {
        CPPA_LOG_TRACE("No gpu devices found. Looking for cpu devices.");
        cout << "No gpu devices found. Looking for cpu devices." << endl;
        dev_type = CL_DEVICE_TYPE_CPU;
        err = clGetDeviceIDs(ids[pid], dev_type, 0, nullptr, &num_devices);
    }
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clGetDeviceIDs: " << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw runtime_error(oss.str());
    }
    vector<cl_device_id> devices(num_devices);
    err = clGetDeviceIDs(ids[pid], dev_type, num_devices, devices.data(), nullptr);
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clGetDeviceIDs: " << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw runtime_error(oss.str());
    }


    // create a context
    m_context.adopt(clCreateContext(0,
                                    devices.size(),
                                    devices.data(),
                                    nullptr,
                                    nullptr,
                                    &err));
    if (err != CL_SUCCESS) {
        ostringstream oss;
        oss << "clCreateContext: " << get_opencl_error(err);
        CPPA_LOGMF(CPPA_ERROR, self, oss.str());
        throw runtime_error(oss.str());
    }


    for (auto& d : devices) {
        CPPA_LOG_TRACE("Creating command queue for device(s).");
        device_ptr device;
        device.adopt(d);
        size_t return_size{0};
        static constexpr size_t buf_size = 128;
        char buf[buf_size];
        err = clGetDeviceInfo(device.get(), CL_DEVICE_NAME, buf_size, buf, &return_size);
        if (err != CL_SUCCESS) {
            CPPA_LOGMF(CPPA_ERROR, self, "clGetDeviceInfo (CL_DEVICE_NAME): "
                                         << get_opencl_error(err));
{'fraction_non_alphanumeric': 0.1420811089257282, 'fraction_numerical': 0.003840029970965627, 'mean_word_length': 1.3920250896057347, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 61, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4882523', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2830, 'n_words': 661}