#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. canvas实现画板功能(渐变、动画、阴影...)

    刚刚在博客园落户了,希望能在这认识更多大神,希望能和大家交好朋友. 闲来无事,把以前用canvas写的画板代码改进了一番,用Html5提供的表单标签给其 加了一个选择颜色的功能,因此发现了该标签的一个 ...

  2. centos6.5 安装python3.5

    1.CentOS6.5 安装Python 的依赖包 yum groupinstall "Development tools" yum install zlib-devel bzip ...

  3. ios微信支付成功后点击左上角返回不走回调的问题

    最近做微信支付发现ios9以后出现的跳转其他app后左上角有返回xxx功能会影响微信支付回调,情况如图 返回后不走下面的方法 - (BOOL)application:(UIApplication *) ...

  4. python代码随笔

    此篇随笔只是作为自己偶然想起的遇到过的代码片段..记录下! 1.巧用lambda,reduce实现多层嵌套的装饰器: 示例如下: #示例 函数chain([a,b,c,d) (input), 最终实现 ...

  5. 快速掌握LODOP打印使用方法

    用运之前简单介绍几个东西. install_lodop32.exe与install_lodop64.exe这两个是2个网页打印控件,网页打印必须安装这个控件在客户端,分为32和64位安装控件.(如果自 ...

  6. 如何让有物理键的手机在ActionBar始终显示更多菜单menu键

    仅作记录代码用,功能未能测试成功,在低版本上不存在 sHasPermanentMenuKey 属性,会出现 java.lang.NoSuchFieldException: sHasPermanentM ...

  7. [转] 你真的会写单例模式吗——Java实现

    你真的会写单例模式吗——Java实现 原文:http://www.tuicool.com/articles/MBrUfy6 单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好.用对单例模 ...

  8. Recursive - leetcode [递归]

    经验tips: Recursion is the best friend of tree-related problems. 一是只要遇到字符串的子序列或配准问题首先考虑动态规划DP,二是只要遇到需要 ...

  9. ECOS-LNMP ZendGuard

    因为ECOS产品加密后依赖PHP Zendguard运行(基于PHP5.3加密)  备注: Shopex485商城基于php5.2进行加密 URL:http://www.zend.com/en/pro ...

  10. 常用mysql命令

    net start mysql命令,启动mysql数据库 1:查看服务器上存在哪些数据库:show databases;2:建立数据库mydb: create database mydb;3:使用你所 ...