1

我是 OpenCL/OneAPI 的新手。如何更改此嵌套循环以使用 oneAPI GPU:

try {
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count; j++) {
                if (a_array[i] * a_array[j] == max) {
                    p_found = a_array[i];
                    q_found = a_array[j];
                    
                    throw "found";
                }
            }
        }
    }
    catch (...) {
        std::cout << "q = " << q_found << " and p = " << p_found << std::endl;
    }
4

1 回答 1

2

以下是该任务的 OpenCL 内核的外观:

#define count 1024
#define max 1.0f
kernel void find(const global float* a_array, gloabl float* pq_found) {
    const uint n = get_global_id(0); // parallelized across nested double loop
    cosnt uint i=n/count, j=n%count;
    const float a_arrayi=a_array[i], a_arrayj=a_array[j];
    if(a_arrayi*a_arrayj==max) {
        pq_found[0] = a_arrayi;
        pq_found[1] = a_arrayj;
    }
}

请注意,由于并行化,有一个小复杂性:如果恰好有一个命中,一切都很好。但是,如果有多个命中,结果将是多个命中中的一个,而它是哪一个将是完全随机的。

于 2020-12-06T20:38:55.400 回答