Sample Code

From ShWiki

(Difference between revisions)
Revision as of 22:00, 13 October 2005
Francois (Talk | contribs)

← Previous diff
Revision as of 22:08, 13 October 2005
Francois (Talk | contribs)
Invite users to suggest new programs
Next diff →
Line 1: Line 1:
 +Here some examples of programs which use Sh. Feel free to suggest additions to this page if you want.
 +
==Simple stream program== ==Simple stream program==

Revision as of 22:08, 13 October 2005

Here some examples of programs which use Sh. Feel free to suggest additions to this page if you want.

Simple stream program

Here is a minimal stream program showing how to create storage for program inputs and outputs.

 #include "sh.hpp"
 #include <iostream>

 using namespace std;
 using namespace SH;

 int main()
 {
   ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") {
     ShInputAttrib3f a;
     ShOutputAttrib3f b;
     b = a + ShAttrib3f(42.0, 42.0, 42.0);
   } SH_END;

   float data[] = { 1.0, 0.5, -0.5 };
   ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT);
   ShChannel<ShAttrib3f> in(mem_in, 1);

   float outdata[3];
   ShHostMemoryPtr mem_out = new ShHostMemory(sizeof(float) * 3, outdata, SH_FLOAT);
   ShChannel<ShAttrib3f> out(mem_out, 1);

   out = prg << in;
  
   float* results = static_cast<float*>(mem_out->hostStorage()->data());
   cout << "out = (" << results[0] << ", " << results[1] << ", "
        << results[2] << ")" << endl;
 }

It should simply output the following:

out = (43, 42.5, 41.5)