Skip to content

Commit ce679de

Browse files
Updated docs
1 parent 6ac2862 commit ce679de

74 files changed

Lines changed: 4405 additions & 714 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc-gen/_site/manifest.json

Lines changed: 761 additions & 22 deletions
Large diffs are not rendered by default.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Alternate methods to execute kernels
2+
3+
In the "Run your first kernel" guide, you have used the executer instance to run the compiler in a simple fashion. Here are more ways to execute and you can choose the way you like
4+
5+
## Using Array For Loop
6+
7+
Let's try to implement some simple neural network activation kernel as below
8+
9+
```csharp
10+
class ForLoopKernels : OpenCLFunctions
11+
{
12+
[OpenCLKernel]
13+
void Sigmoid([Global]float[] x)
14+
{
15+
int i = get_global_id(0);
16+
x[i] = exp(x[i]) / (exp(x[i]) + 1);
17+
}
18+
19+
[OpenCLKernel]
20+
void Threshold([Global] float[] x, float value)
21+
{
22+
int i = get_global_id(0);
23+
x[i] = x[i] > value ? 1 : 0;
24+
}
25+
}
26+
```
27+
28+
And if we need to invoke using AmplifyFor loop which is an extension method for an array, here is the below code:
29+
30+
```csharp
31+
public void Execute()
32+
{
33+
//Create instance of OpenCL compiler and use device
34+
var compiler1 = new OpenCLCompiler();
35+
compiler1.UseDevice(0);
36+
37+
var compiler2 = new OpenCLCompiler();
38+
compiler1.UseDevice(1);
39+
40+
compiler1.CompileKernel(typeof(ForLoopKernels));
41+
compiler2.CompileKernel(typeof(ForLoopKernels));
42+
43+
float[] x = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
44+
45+
x.AmplifyFor(compiler1, "Sigmoid");
46+
PrintArray(x);
47+
48+
Console.WriteLine();
49+
50+
x.AmplifyFor(compiler2, "Threshold", 0.85f);
51+
PrintArray(x);
52+
}
53+
54+
private void PrintArray(float[] data)
55+
{
56+
for(int i=0;i<data.Length;i++)
57+
{
58+
Console.Write(data[i] + " ");
59+
}
60+
}
61+
```
62+
63+
## Using compiler execute function
64+
65+
The compiler execute function is the base for all the other kernel invoke. Below is the sample implementation for the same example we have above:
66+
67+
```csharp
68+
public void Execute()
69+
{
70+
//Create instance of OpenCL compiler and use device
71+
var compiler1 = new OpenCLCompiler();
72+
compiler1.UseDevice(0);
73+
74+
var compiler2 = new OpenCLCompiler();
75+
compiler1.UseDevice(1);
76+
77+
compiler1.CompileKernel(typeof(ForLoopKernels));
78+
compiler2.CompileKernel(typeof(ForLoopKernels));
79+
80+
float[] x = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
81+
82+
compiler1.Execute<float>("Sigmoid", x);
83+
PrintArray(x);
84+
85+
Console.WriteLine();
86+
87+
compiler2.Execute<float>("Threshold", x, 0.85f);
88+
PrintArray(x);
89+
}
90+
91+
private void PrintArray(float[] data)
92+
{
93+
for(int i=0;i<data.Length;i++)
94+
{
95+
Console.Write(data[i] + " ");
96+
}
97+
}
98+
```

doc-gen/articles/Save and load.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Saving and loading the compiler
2+
3+
Saving the compiler after compiling the kernel and reusing it is an efficient way of working. This save a lot of compilation time and the saved bin file can be reused in other project.
4+
5+
Below is how you can save a compiler after compiling with kernel class. Simply invoke Save function passing the file name:
6+
7+
```csharp
8+
private void SaveCompiler()
9+
{
10+
//Create instance of OpenCL compiler
11+
var compiler = new OpenCLCompiler();
12+
13+
//Select a default device
14+
compiler.UseDevice(0);
15+
16+
//Compile the sample kernel
17+
compiler.CompileKernel(typeof(SimpleKernels));
18+
compiler.Save("amp_cl.bin");
19+
}
20+
```
21+
22+
Create another project file, and you can copy the amp_cl.bin file to the new project directory. Below is the code to load and execute, note that you don't have to compile anything here.
23+
24+
```csharp
25+
//Once saved you can reuse the same bin instead of compiling from scratch. Save compilation time. Also the bin file is portable
26+
var compiler = new OpenCLCompiler();
27+
compiler.Load("amp_cl.bin");
28+
29+
foreach (var item in compiler.Kernels)
30+
{
31+
Console.WriteLine(item);
32+
}
33+
34+
//Create variable a, b and r
35+
Array x = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
36+
Array y = new float[9];
37+
38+
//Get the execution engine
39+
var exec = compiler.GetExec<float>();
40+
41+
//Execute fill kernel method
42+
exec.Fill(y, 0.5f);
43+
44+
//Execuete SAXPY kernel method
45+
exec.SAXPY(x, y, 2f);
46+
47+
//Print the result
48+
Console.WriteLine("\nResult----");
49+
for (int i = 0; i < y.Length; i++)
50+
{
51+
Console.Write(y.GetValue(i) + " ");
52+
}
53+
```

doc-gen/articles/Useful functions.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ Following are the list of useful functions to build and run the kernel
1111
2. args: List of arguments array or scalar values.
1212

1313
6. **GetExecuter**: Get the executer for the compiler which can seemlesly invoke the kernel methods. Internally it calls the compiler Execute function.
14+
15+
7. **Save**: Save the compiler in form of binary for reusablibity.
16+
17+
8. **Load**: Load the saved bin compiler file. Avoids recompilation of kernel.

doc-gen/articles/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66
href: Create a simple kernel.md
77
- name: Run your first kernel
88
href: Run your first kernel.md
9+
- name: Alternate methods to execute kernels
10+
href: Execute Kernel Alternatives.md
11+
- name: Saving and loading compiler
12+
href: Save and load.md

0 commit comments

Comments
 (0)