-3
A
回答
1
這樣的事情應該工作。爲了簡潔起見,我簡化了通常的cuda錯誤檢查。
#include <stdio.h>
#define DSIZE 10000
#define nTPB 512
__global__ void cmp(float *a, float *b, int size){
int idx = threadIdx.x + blockDim.x*blockIdx.x;
if (idx < size)
a[idx]=(a[idx] > b[idx])?1.0f:0.0f; // could also be: ?(a[idx]/a[idx]):0;
}
int main() {
cudaError_t err;
float *h_a, *h_b, *d_a, *d_b;
h_a = (float *)malloc(DSIZE*sizeof(float));
if (h_a == 0) {printf("malloc fail\n"); return 1;}
h_b = (float *)malloc(DSIZE*sizeof(float));
if (h_b == 0) {printf("malloc fail\n"); return 1;}
for (int i=0; i< DSIZE; i++){
h_a[i] = 10.0f;
h_b[i] = (float)i;}
err = cudaMalloc((void **)&d_a, DSIZE*sizeof(float));
if (err != cudaSuccess) {printf("cuda fail\n"); return 1;}
err = cudaMalloc((void **)&d_b, DSIZE*sizeof(float));
if (err != cudaSuccess) {printf("cuda fail\n"); return 1;}
err = cudaMemcpy(d_a, h_a, DSIZE*sizeof(float), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {printf("cuda fail\n"); return 1;}
err = cudaMemcpy(d_b, h_b, DSIZE*sizeof(float), cudaMemcpyHostToDevice);
if (err != cudaSuccess) {printf("cuda fail\n"); return 1;}
cmp<<<(DSIZE+nTPB-1)/nTPB, nTPB>>>(d_a, d_b, DSIZE);
err=cudaMemcpy(h_a, d_a, DSIZE*sizeof(float), cudaMemcpyDeviceToHost);
if (err != cudaSuccess) {printf("cuda fail\n"); return 1;}
for (int i=0; i< 20; i++)
printf("h_a[%d] = %f\n", i, h_a[i]);
return 0;
}
相關問題
- 1. MySQL:Array comparsion
- 2. OpenCV Image comparsion
- 3. Oracle Date Comparsion
- 4. C#字符串comparsion方法
- 5. 這是本comparsion操作
- 6. 如何檢查時間comparsion在MySQL
- 7. Comparsion奇怪的行爲0 ==「一」
- 8. 在CUDA編程中使用基於窗體的Cuda的Cuda
- 9. 測試在CUDA
- 10. 可以在CUDA
- 11. 同步在CUDA
- 12. CUDA-GDB:沒有CUDA設備
- 13. CUDA 5與CUDA 4兼容
- 14. CUDA,在共享memroy
- 15. 比較法違反其總承包,長comparsion
- 16. 命令未找到錯誤的,如果[-gt] comparsion
- 17. Nvidia Cuda計劃 - 我的Cuda適合Cuda架構嗎?
- 18. 對CUDA
- 19. 對CUDA
- 20. 與CUDA
- 21. CUDA __threadfence()
- 22. 對CUDA
- 23. 與CUDA
- 24. 在非CUDA機器上運行CUDA程序
- 25. CUDA/PyCUDA:診斷在cuda-gdb下消失的啓動失敗
- 26. 如何在cpp和cuda模塊中使用cuda類頭文件
- 27. cuda函數在cuda中的元素應用
- 28. 二維數組CUDA 2D紋理CUDA
- 29. 複製到CUDA到設備內存CUDA
- 30. cuda 8.0中'cuda-memcheck'的錯誤
什麼呢A = A/A是什麼意思?那不就是說a = 1嗎? – 2013-03-15 04:43:47
是A = A/A表示1 ,,基本上如果A [1]> B [I] ,,我想要返回值1,否則爲0,這我想要重複對所有的索引值(比方說20) – Ani 2013-03-15 06:36:27
先生,基本上我正在使用CUFFT庫。因爲我有兩個輸出,我必須比較4096個索引值。所以PLZ指導我。 – Ani 2013-03-15 06:55:20