cuda多线程间通信
#include "cuda_runtime.h"
#include "device_launch_parameters.h" #include <stdio.h>
#include <time.h>
#include <stdlib.h> #define MAX 120
#define MIN 0
cudaError_t addWithCuda(int *c, const int *a, size_t size); __global__ void addKernel(int *c, const int *a) {
int i = threadIdx.x;
extern __shared__ int smem[];
smem[i] = a[i];
__syncthreads();
if (i == ) // 0号线程做平方和
{
c[] = ;
for (int d = ; d < ; d++) {
c[] += smem[d] * smem[d];
}
}
if (i == ) //1号线程做累加
{
c[] = ;
for (int d = ; d < ; d++) {
c[] += smem[d];
}
}
if (i == ) //2号线程做累乘
{
c[] = ;
for (int d = ; d < ; d++) {
c[] = smem[d];
} } if (i == ) //3号线程做异或
{
c[] = ;
for (int d = ; d < ; d++) {
c[] ^= smem[d];
} }
} int main() {
const int arraySize = ;
srand((unsigned) time(NULL));
const int a[arraySize] = { rand() % (MAX + - MIN) + MIN, rand()
% (MAX + - MIN) + MIN, rand() % (MAX + - MIN) + MIN, rand()
% (MAX + - MIN) + MIN, rand() % (MAX + - MIN) + MIN };
int c[arraySize] = { };
// Add vectors in parallel.
cudaError_t cudaStatus = addWithCuda(c, a, arraySize);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "addWithCuda failed!");
return ;
}
printf(
"\t%d+%d+%d+%d+%d = %d\n\t%d^2+%d^2+%d^2+%d^2+%d^2 = %d\n\t%d*%d*%d*%d*%d = %d\n\t%d^%d^%d^%d^%d = %d\n\n\n\n\n",
a[], a[], a[], a[], a[], c[], a[], a[], a[], a[], a[],
c[], a[], a[], a[], a[], a[], c[],a[], a[], a[], a[], a[], c[]);
// cudaThreadExit must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaStatus = cudaThreadExit();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaThreadExit failed!");
return ;
}
return ;
} // Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int *c, const int *a, size_t size) {
int *dev_a = ;
int *dev_c = ;
cudaError_t cudaStatus; // Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice();
if (cudaStatus != cudaSuccess) {
fprintf(stderr,
"cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
} // Allocate GPU buffers for three vectors (two input, one output) .
cudaStatus = cudaMalloc((void**) &dev_c, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
} cudaStatus = cudaMalloc((void**) &dev_a, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
// Copy input vectors from host memory to GPU buffers.
cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int),
cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
// Launch a kernel on the GPU with one thread for each element.
addKernel<<<, size, size * sizeof(int), >>>(dev_c, dev_a); // cudaThreadSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaThreadSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr,
"cudaThreadSynchronize returned error code %d after launching addKernel!\n",
cudaStatus);
goto Error;
} // Copy output vector from GPU buffer to host memory.
cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int),
cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
} Error: cudaFree(dev_c);
cudaFree(dev_a);
return cudaStatus;
}
22+103+61+63+17 = 266
	22^2+103^2+61^2+63^2+17^2 = 19072
	22*103*61*63*17 = 17
	22^103^61^63^17 = 98
cuda多线程间通信的更多相关文章
- (十一)boost库之多线程间通信
		
(十一)boost库之多线程间通信 1.互斥锁 在编程中,引入了对象互斥锁的概念,来保证共享数据操作的完整性.每个对象都对应于一个可称为" 互斥锁" 的标记,这个标记用来保证在任一 ...
 - Java 多线程间通信
		
JDK 1.5 以后, 将同步和锁封装成了对象, 并将操作锁的隐式方法定义到了该对象中, 将隐式动作变成了显示动作. Lock 接口 Lock 接口, 位于 java.util.concurrent. ...
 - Java多线程间通信-解决安全问题、等待唤醒机制
		
/*1.增加一个知识点一个类怎么在所有的类中,让其它类来共同修改它的数据呢?可以用单例设计模式可以用静态可以在其它类中做一个构造函数,接受同一个对象,这样就可以实现对象 2.状态选择可以用数字0 1 ...
 - 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析和开发示例
		
AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...
 - java 多线程间通信(二)
		
传统的线程通信 Object提供了三个方法wait(), notify(), notifyAll()在线程之间进行通信,以此来解决线程间执行顺序等问题. wait():释放当前线程的同步监视控制器,并 ...
 - 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析
		
AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...
 - java 多线程间通信(一)
		
synchronized同步 package com.test7; public class Run { public class MyObject { private int a; public M ...
 - wxpython多线程间通信
		
#!bin/bash/python # -*- coding=utf-8 -*- import time import wx from threading import Thread from wx. ...
 - 06_Java多线程、线程间通信
		
1. 线程的概念 1.1多进程与多线程 进程:一个正在执行的程序.每个进程执行都有一个执行顺序,该顺序是一个执行路径,或叫一个控制单元. 一个进程至少有一个线程. 线程:就是进程中的一个独立 ...
 
随机推荐
- Seen.js – 使用 SVG 或者 Canvas 渲染 3D 场景
			
Seen.js 渲染3D场景为 SVG 或者 HTML5 画布.Seen.js 包含对于 SVG 和 HTML5 Canvas 元素的图形功能的最简单的抽象.所有这个库的其它组件都是不用关心将要渲染的 ...
 - 记STM32F030多通道ADC DMA读取乱序问题
			
问题描述通过 uint16_t ConvData[8]保存DMA搬运的ADC转换数值,但是这个数组数值的顺序总是和ADC不是顺序对应的.比如用7个通道的ADC,当设置ADC_InitStructure ...
 - JavaScript MVC框架和语言总结[infoq]
			
infoq关于javascript的语言和框架的总结,非常全面,值得一读. http://www.infoq.com/minibooks/emag-javascript Contents of the ...
 - Android  国际化
			
由于公司的项目是投放 google play store , 所以要做国际化.国际化遇到的两个大问题 字符串国际化 布局样式国际化 一:字符串国际化 解决这个问题很简单,在res目录下放 ...
 - Android工程师常见面试题集答案
			
13.描述一下Android的系统结构? android系统架构分从下往上为linux 内核层.运行库.应用程序框架层.和应用程序层. linuxkernel:负责硬件的驱动程序.网络.电源.系统安全 ...
 - 【代码笔记】iOS-翻书效果的实现
			
代码: RootViewController.m #import "RootViewController.h" @interface RootViewController () @ ...
 - android app设计内容
			
1.logo-main-模块页: 2.logo页包括:背景.版本号.图标.软件名.blog等内容:
 - Swift中的部分更新与旧版的区别
			
1. 函数中的外部变量名取消 “#”方式,仅能用直接命名方式 错误 func swift(#str :NSString){} 正确 func swift(str str :NSString){} 2. ...
 - JSON TO NSDictionary  Mac & iOS
			
NSString * jsonPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Cont ...
 - apache 虚拟ip
			
参考 http://blog.sina.com.cn/s/blog_5d8ca1e90100hnpv.html <VirtualHost 127.0.0.1:80> Docume ...