#include <osg/Notify>
#include <osgViewer/Viewer>
#include <osgCompute/Memory>
#include <osgCompute/Module>
#include <osgCuda/Memory>
#include <memory.h>
#include <iostream>
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgComputed.lib")
#pragma comment(lib, "osgCudad.lib")
#pragma comment(lib, "osgd.lib")
extern "C"
void MyCudaTest(unsigned int numBlocks, unsigned int numThreads, void * bytes1, void * bytes2, void * bytes3);
class MyModule : public osgCompute::Module
{
public:
MyModule()
:osgCompute::Module()
{
clearLocal();
}
META_Object(osgCompute,MyModule)
virtual bool init()
{
_numThreads = ;
_numBlocks = _buffer1->getDimension()/_numThreads;
return osgCompute::Module::init();
} virtual void clear()
{
clearLocal();
osgCompute::Module::clear();
} virtual void launch()
{
MyCudaTest(_numBlocks, _numThreads, _buffer1->map(), _buffer2->map(), _buffer3->map());
} inline void setBuffer1(osgCompute::Memory * buffer)
{
_buffer1 = buffer;
}
inline void setBuffer2(osgCompute::Memory * buffer)
{
_buffer2 = buffer;
}
inline void setBuffer3(osgCompute::Memory * buffer)
{
_buffer3 = buffer;
}
protected:
virtual ~MyModule()
{
clearLocal();
} virtual void clearLocal()
{
_buffer1 = NULL;
_buffer2 = NULL;
_buffer3 = NULL;
} protected:
unsigned int _numThreads;
unsigned int _numBlocks;
osg::ref_ptr<osgCompute::Memory> _buffer1;
osg::ref_ptr<osgCompute::Memory> _buffer2;
osg::ref_ptr<osgCompute::Memory> _buffer3;
private:
MyModule(const MyModule &, const osg::CopyOp &)
{ }
inline MyModule & operator = (const MyModule &)
{
return *this;
}
}; int main()
{
unsigned int a[] = {, , };
unsigned int b[] = {, , };
unsigned int c[] = {, , }; unsigned int num = sizeof(a)/sizeof(unsigned int); osg::ref_ptr<osgCuda::Memory> buffer1 = new osgCuda::Memory;
buffer1->setElementSize(sizeof(int));
buffer1->setDimension(, num);
buffer1->init(); osg::ref_ptr<osgCuda::Memory> buffer2 = new osgCuda::Memory;
buffer2->setElementSize(sizeof(int));
buffer2->setDimension(, num);
buffer2->init(); osg::ref_ptr<osgCuda::Memory> buffer3 = new osgCuda::Memory;
buffer3->setElementSize(sizeof(int));
buffer3->setDimension(, num);
buffer3->init(); osg::ref_ptr<MyModule> module = new MyModule;
module->setBuffer1(buffer1.get());
module->setBuffer2(buffer2.get());
module->setBuffer3(buffer3.get());
module->init(); unsigned int * bufferPtr1 = (unsigned int *)(buffer1->map(osgCompute::MAP_HOST_TARGET));
memcpy(bufferPtr1, a, sizeof(a)); unsigned int * bufferPtr2 = (unsigned int *)(buffer2->map(osgCompute::MAP_HOST_TARGET));
memcpy(bufferPtr2, b, sizeof(b)); unsigned int * bufferPtr3 = (unsigned int *)(buffer3->map(osgCompute::MAP_HOST_TARGET));
memcpy(bufferPtr3, c, sizeof(c));
module->launch(); bufferPtr3 = (unsigned int *)(buffer3->map(osgCompute::MAP_HOST_SOURCE));
std::cout << "并行计算:(1, 2, 3)于(4, 5, 6)的和:"<< std::endl;
for (unsigned int i = ; i < buffer3->getDimension(); ++i)
{
std::cout << bufferPtr3[i] << std::endl;
} }
#include <cuda_runtime.h>
__global__ void addKernel(int * bytes1, int * bytes2, int * bytes3)
{
int tid = blockIdx.x;
bytes3[tid] = bytes2[tid] + bytes1[tid];
}
extern "C"
void MyCudaTest(unsigned int numBlocks, unsigned int numThreads, void * bytes1, void * bytes2, void * bytes3)
{
addKernel<<<numBlocks, numThreads>>>((int *)bytes1, (int *)bytes2, (int *)bytes3);
}

osg + cuda的更多相关文章

  1. 朱丽叶—Cuda+OSG

    #include <cuda_runtime.h> #include <osg/Image> ; typedef struct cuComplex { float r; flo ...

  2. CUDA[2] Hello,World

    Section 0:Hello,World 这次我们亲自尝试一下如何用粗(CU)大(DA)写程序 CUDA最新版本是7.5,然而即使是最新版本也不兼容VS2015 ...推荐使用VS2012 进入VS ...

  3. CUDA[1] Introductory

    Section 0 :Induction of CUDA CUDA是啥?CUDA®: A General-Purpose Parallel Computing Platform and Program ...

  4. Couldn't open CUDA library cublas64_80.dll etc. tensorflow-gpu on windows

    I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_load ...

  5. OSG计时器与时间戳

    static osg::Timer* sendMsgTimer = new osg::Timer; if (sendMsgTimer->time_m()>100)//100ms {// d ...

  6. ubuntu 16.04 + N驱动安装 +CUDA+Qt5 + opencv

    Nvidia driver installation(after download XX.run installation file) 1. ctrl+Alt+F1   //go to virtual ...

  7. OSG消息机制之消息分析

    OSG消息接收在头文件有各种事件的相关参数

  8. OSG消息机制之事件处理概述

    OSG的消息机制包括好多个头文件预定义及多个类. 首先,消息接收相关的类当属osgGA::GUIEventHandler和osgGA::GUIEventAdapter这两个类了.前者处理OSG程序与用 ...

  9. OSG 3D场景渲染编程概述

    OSG是Open Scene Graphic的缩写,是基于C++平台的使用OpenGL技术的开源3D场景开发. vs环境安装或者是在Ubuntu中环境的安装网上教程很多,都是大同小异的,认真操作容易成 ...

随机推荐

  1. spring mvc @ResponseStatus 注解 注释返回中文乱码的问题

    前言 前文中讲到,使用@ResponseStatus注解,可以修饰一个异常类,在发生异常的时候返回指定的错误码和消息,在返回的 reason中包含中文的时候,就会出现中文乱码的问题 现象 reason ...

  2. 免备案速度快最新优惠码,vps评测digitalocean对比vultr和linode

    在无数海外vps服务器供应商中,vultr价格便宜,有日本机房不限购,对中国大陆速度友好:linode是经典款,服务器最稳定,内存翻倍,起步就是2GB,性价比高:digitalocean服务器创建速度 ...

  3. 终于了解了User-Agent的历史了

    你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? 1 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.3 ...

  4. .net mvc 超过了最大请求长度 限制文件上传大小

    在我们的项目中遇到"超过了最大请求长度"如下图所示,是因为IIS默认请求长度4M,当请求长度大于这个值的时候报错,下面是解决方案. 解决方案:修改web.config文件 1.注意 ...

  5. CSS样式类型

    CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式 (一)内联式样式 内联式css样式表就是把css代码直接写在现有 ...

  6. Windows 多用户远程访问 Ubuntu 14.04桌面

    使用X2Go实现多用户远程访问 Ubuntu 14.04桌面:VNC也可以,但是每次连接VNC就回新创建一个Seession,想要在下次远程登录的时候返回上次活动,需要记住开启的线程,这种繁琐的操作不 ...

  7. ansible 判断和循环

    标准循环 模式一 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - te ...

  8. C#拾遗(一、基本类型)

    1. C#是一种块结构语言,用花括号{}分块,但是用#region和#endregion来定义可以展开和折叠的代码区域 #region 这是引用区 using System; ...... #endr ...

  9. H3C SecPath 防火墙端口回流

    外网IP地址可以通过域名访问内外800端口网站,内网地址不可以,需要做回流 <<内网口.外网口都需要做nat映射>>定义ACL3001acl number 3001rule 0 ...

  10. ef code first 您没有所需权限

    在进行数据库更新操作时,update-database -force,反复提示没有改表或者没有所需权限,昨晚折腾了4个小时没有搞定,太晚了,今天Google了一下,这方面的错误,提到的都很少,在一篇文 ...