我的情况:warp中的每个线程都是独立运行的。不同的数据阵列。所有线程都在其数据数组上循环。每个线程的循环迭代次数不同。 (这会产生费用,我知道。)
在for循环中,每个线程在计算三个浮点数后需要保存最大值。在for循环之后,warp中的线程将通过检查由 warp 中的“相邻线程”计算的最大值来“通信”(由奇偶校验确定)。
问题:
示例代码:
__shared__ int N_per_data; // loaded from host
__shared__ float ** data; //loaded from host
data = new float*[num_threads_in_warp];
for (int j = 0; j < num_threads_in_warp; ++j)
data[j] = new float[N_per_data[j]];
// the values of jagged matrix "data" are loaded from host.
__shared__ float **max_data = new float*[num_threads_in_warp];
for (int j = 0; j < num_threads_in_warp; ++j)
max_data[j] = new float[N_per_data[j]];
for (uint j = 0; j < N_per_data[threadIdx.x]; ++j)
{
const float a = f(data[threadIdx.x][j]);
const float b = g(data[threadIdx.x][j]);
const float c = h(data[threadIdx.x][j]);
const int cond_a = (a > b) && (a > c);
const int cond_b = (b > a) && (b > c);
const int cond_c = (c > a) && (c > b);
// avoid if-statements. question (1) and (2)
max_data[threadIdx.x][j] = conda_a * a + cond_b * b + cond_c * c;
}
// Question (3):
// No "syncthreads" necessary in next line:
// access data of your mate at some magic positions (assume it exists):
float my_neighbors_max_at_7 = max_data[threadIdx.x + pow(-1,(threadIdx.x % 2) == 1) ][7];
在GPU上实现我的算法之前,我正在研究算法的各个方面,以确保它值得实施。所以请耐心等待..
答案 0 :(得分:1)