我们如何通过推力从全球访问指针。
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentTitle(getString(R.string.notification))
.setContentText(getString(R.string.ping))
.setDefaults(Notification.DEFAULT_ALL);
我可以在全局
中使用send_data与...合作。我无法检索它
thrust::device_vector<int> testvec;
int *send_data = thrust::raw_pointer_cast(&testvec[0]);
<< <1, 1 >> > (send_data, raw_ptr );
答案 0 :(得分:2)
有许多方法可以从主机访问设备数据。你可以:
thrust::copy
cudaMemcpy
以下代码演示了这四种方法 - 我确定还有其他方法或变体:
$ cat t897.cu
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#define DSIZE 10
__global__ void kernel(int *send_data, int sz){
int idx = threadIdx.x+blockDim.x*blockIdx.x;
if (idx < sz) send_data[idx]++;
}
int main(){
thrust::device_vector<int> testvec(DSIZE);
int *send_data = thrust::raw_pointer_cast(&(testvec[0]));
kernel<<<1,DSIZE>>>(send_data, DSIZE);
// method 1
thrust::host_vector<int> hvec1 = testvec;
for (int i = 0; i < DSIZE; i++) printf("%d: %d\n", i, hvec1[i]);
// method 2
thrust::host_vector<int> hvec2(DSIZE);
thrust::copy(testvec.begin(), testvec.end(), hvec2.begin());
for (int i = 0; i < DSIZE; i++) printf("%d: %d\n", i, hvec2[i]);
// method 3
int *hvec3 = (int *)malloc(DSIZE*sizeof(int));
cudaMemcpy(hvec3, send_data, DSIZE*sizeof(int), cudaMemcpyDeviceToHost);
for (int i = 0; i < DSIZE; i++) printf("%d: %d\n", i, hvec3[i]);
// method 4
for (int i = 0; i < DSIZE; i++) { int temp = testvec[i]; printf("%d: %d\n", i, temp);}
}
$ nvcc -o t897 t897.cu
$ ./t897
0: 1
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1
7: 1
8: 1
9: 1
0: 1
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1
7: 1
8: 1
9: 1
0: 1
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1
7: 1
8: 1
9: 1
0: 1
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1
7: 1
8: 1
9: 1
$