当前位置:网站首页>CUDA realizes efficient search - failed audit?
CUDA realizes efficient search - failed audit?
2022-06-10 17:47:00 【Wu lele~】
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
void __global__ bisearch(int *d, int *dval, int *pos, const int N);
void __global__ bisearch(int *d, int *dval, int *pos, const int N)
{
int tid = blockDim.x * blockIdx.x + threadIdx.x;
if(tid < N)
{
if(d[tid] == *dval)
{
*pos = tid;}
}
}
int main(int argc, char *arcv[])
{
if(argc<3)
{
printf("assignment is not enough!");
}
int N = atoi(arcv[1]);
int val = atoi(arcv[2]);
// host memory
int hpos = -1;
int a[N] = {
0,1,2};
vector<int> vec(a,a+N);
/* for(int i=0; i<N; i++) { vec.push_back(i); } cout<< vec[0] <<endl; cout<< vec[1] <<endl; cout<< vec[2] << endl; cout<< val<<endl; */
int *h;
h = (int *)&vec[0];
// device memory
int *d;
cudaMalloc(&d, sizeof(int)*N);
int *dval;
cudaMalloc(&dval, sizeof(int));
int *dpos;
cudaMalloc(&dpos, sizeof(int));
// host --> device
cudaMemcpy( d, h, sizeof(int)*N,cudaMemcpyHostToDevice);
cudaMemcpy( dval, &val, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy( dpos, &hpos, sizeof(int), cudaMemcpyHostToDevice);
// threads and blocks
const int block_size = 128;
const int grid_size = (N + block_size -1) / block_size;
bisearch<<<grid_size,block_size>>>(d, dval, dpos, N);
// cuda to cpu
cudaMemcpy( &hpos, dpos, sizeof(int), cudaMemcpyDeviceToHost );
printf("Ans: %d\n", hpos);
// free memory
cudaFree(d);
cudaFree(dval);
cudaFree(dpos);
return 0;
}
summary
vector I don't know how to use it ;vector The definition requires the use of using namespace std;
边栏推荐
- 海外数据中心需要为不可预测的灾难做好准备
- 2022年焊工(初级)试题模拟考试平台操作
- pands pd. Detailed parsing of dataframe() function
- com.netflix.client.ClientException: Load balancer does not have available server for client: userser
- Nat. Rev. Drug Discov. | Application of AI in small molecule drug discovery: an upcoming wave?
- IPO治不了威马的杂症?
- 2022年T电梯修理考试题模拟考试题库及在线模拟考试
- 几个对程序员的误解,害人不浅!
- 当v-if和v-for需要同时使用的时候
- 软件项目管理 6.10.成本预算
猜你喜欢

《华为数据之道》读书笔记

Solve the problem that idea is stuck in opening a project

Chongqing's first sci tech Innovation Board IPO is coming

com.netflix.client.ClientException: Load balancer does not have available server for client: userser

Full link tracking & performance monitoring tool skywalking practice

SVN中的回退操作

matplotlib plt.text()的具体用法——画图时给图中的点加标签

Talk about message oriented middleware (1) and AMQP

Why 0.1+0.2=0.3000000000000004

Numpy numpy中np.set_printoptions()的用法——控制输出方式
随机推荐
信息学奥赛一本通 1280:【例9.24】滑雪 | OpenJudge NOI 2.6 90:滑雪 | 洛谷 P1434 [SHOI2002] 滑雪
It has become a unicorn since its establishment one year ago. Tencent didi is the "backer". This year's new unicorn is not simple
OpenJudge NOI 1.13 15:求序列中的众数
每日一题-1287. 有序数组中出现次数超过25%的元素
Nat. Rev. Drug Discov. | Application of AI in small molecule drug discovery: an upcoming wave?
Fabric. JS lock the background image, not affected by zooming and dragging
How will you integrate into the $20trillion "project economy" in five years
2022G1工业锅炉司炉考题及在线模拟考试
flex布局语法
自定义视图:图形与图像的处理(一):使用简单图片
【内容共创】第17期 夏日炎炎,有你超甜!参与签约华为云小编,花样礼包总有一款打动你!
高数_第6章无穷级数__绝对收敛_条件收敛
vscode常用快捷键
Gateway服务网关
Leetcode 929. 独特的电子邮件地址
The children in the deep mountains are seeing the stars farther away
How MySQL modifies field type and field length
解决 vs2022在调试程序时缓慢加载一堆符号的问题
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
域名备案和icp备案有哪些区别?