Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dead link on the Advanced Topics page in the documentation. #886

Open
fishnet37222 opened this issue Aug 6, 2024 · 1 comment
Open

Dead link on the Advanced Topics page in the documentation. #886

fishnet37222 opened this issue Aug 6, 2024 · 1 comment

Comments

@fishnet37222
Copy link

The "Advanced Topics" page in the documentation contains a link to a blog post that no longer exists. The link on the page is "Custom OpenCL functions in C++ with Boost.Compute". If you navigate to that link, you'll see it no longer exists. However, I was able to find the blog post in the Internet Archive: https://web.archive.org/web/20201112042827/http://kylelutz.blogspot.com/2014/03/custom-opencl-functions-in-c-with.html.

@ljluestc
Copy link


#include <iostream>
#include <vector>
#include <boost/compute.hpp>

namespace compute = boost::compute;

int main() {
    // Select the default OpenCL device
    compute::device device = compute::system::default_device();
    compute::context context(device);
    compute::command_queue queue(context, device);

    std::cout << "Using OpenCL device: " << device.name() << std::endl;

    // OpenCL kernel source (custom function)
    const char source[] = R"(
        __kernel void square(__global float *data) {
            int gid = get_global_id(0);
            data[gid] = data[gid] * data[gid]; // Square each element
        }
    )";

    // Build OpenCL program and create kernel
    compute::program program = compute::program::build_with_source(source, context);
    compute::kernel kernel(program, "square");

    // Create input data
    std::vector<float> host_data = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
    compute::vector<float> device_data(host_data.size(), context);

    // Copy data to device
    compute::copy(host_data.begin(), host_data.end(), device_data.begin(), queue);

    // Set kernel argument and execute
    kernel.set_arg(0, device_data);
    queue.enqueue_1d_range_kernel(kernel, 0, host_data.size(), 0);

    // Copy results back to host
    compute::copy(device_data.begin(), device_data.end(), host_data.begin(), queue);

    // Print results
    std::cout << "Squared Values:" << std::endl;
    for (float val : host_data) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants