2016-10-02 308 views

回答

0

這段代碼會從一個陣列並行元素添加到另一個,使用原子操作進行安全。

/* 
* list: is of size some size greater than a, one thread per element of list 
* a: is of size "size", initially 0 
* size: this is the size of array "a", initial value is 0 
* capacity: this is the number of elements allocated for "a" 
*/ 
__kernel void AddElementsToEndOfArray(
     __global int* list, 
     __global int* a, 
     __global int size, 
     __global int capacity) 
{ 
    local int sz = atomic_add(&(size),1); 
    if (sz >= capacity) 
     return; 

    unsigned int i = get_global_id(0); 
    a[sz] = list[i]; 
} 
+0

這可能會實現,但考慮到以下內容:「線程在同一經線也執行內存訪問在一起,當兩個或多個線程同時寫入同一個位置,CUDA保證一個任意線程會成功。 「 –

相關問題