Sample Code

From ShWiki

(Difference between revisions)
Revision as of 22:08, 13 October 2005
Francois (Talk | contribs)
Missing word
← Previous diff
Revision as of 14:56, 10 December 2005
Francois (Talk | contribs)
Format c++ code nicely
Next diff →
Line 4: Line 4:
Here is a minimal stream program showing how to create storage for program inputs and outputs. Here is a minimal stream program showing how to create storage for program inputs and outputs.
 +<cpp>#include "sh.hpp"
 +#include <iostream>
-<pre><nowiki>+using namespace std;
- #include "sh.hpp"+using namespace SH;
- #include <iostream>+
- using namespace std;+int main()
- using namespace SH;+{
 + ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") {
 + ShInputAttrib3f a;
 + ShOutputAttrib3f b;
 + b = a + ShAttrib3f(42.0, 42.0, 42.0);
 + } SH_END;
- int main()+ float data[] = { 1.0, 0.5, -0.5 };
- {+ ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT);
- ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") {+ ShChannel<ShAttrib3f> in(mem_in, 1);
- ShInputAttrib3f a;+
- ShOutputAttrib3f b;+
- b = a + ShAttrib3f(42.0, 42.0, 42.0);+
- } SH_END;+
- float data[] = { 1.0, 0.5, -0.5 };+ float outdata[3];
- ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT);+ ShHostMemoryPtr mem_out = new ShHostMemory(sizeof(float) * 3, outdata, SH_FLOAT);
- ShChannel<ShAttrib3f> in(mem_in, 1);+ ShChannel<ShAttrib3f> out(mem_out, 1);
- float outdata[3];+ out = prg << in;
- 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());+ float* results = static_cast<float*>(mem_out->hostStorage()->data());
- cout << "out = (" << results[0] << ", " << results[1] << ", "+ cout << "out = (" << results[0] << ", " << results[1] << ", "
- << results[2] << ")" << endl;+ << results[2] << ")" << endl;
- }+}</cpp>
-</nowiki></pre>+
It should simply output the following: It should simply output the following:
out = (43, 42.5, 41.5) out = (43, 42.5, 41.5)

Revision as of 14:56, 10 December 2005

Here are 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.

  1. #include "sh.hpp"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. using namespace SH;
  6.  
  7. int main()
  8. {
  9. ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") {
  10. ShInputAttrib3f a;
  11. ShOutputAttrib3f b;
  12. b = a + ShAttrib3f(42.0, 42.0, 42.0);
  13. } SH_END;
  14.  
  15. float data[] = { 1.0, 0.5, -0.5 };
  16. ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT);
  17. ShChannel<ShAttrib3f> in(mem_in, 1);
  18.  
  19. float outdata[3];
  20. ShHostMemoryPtr mem_out = new ShHostMemory(sizeof(float) * 3, outdata, SH_FLOAT);
  21. ShChannel<ShAttrib3f> out(mem_out, 1);
  22.  
  23. out = prg << in;
  24. float* results = static_cast<float*>(mem_out->hostStorage()->data());
  25. cout << "out = (" << results[0] << ", " << results[1] << ", "
  26. << results[2] << ")" << endl;
  27. }

It should simply output the following:

out = (43, 42.5, 41.5)