forked from paladin-t/c4gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog.cs
131 lines (113 loc) · 3.08 KB
/
prog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
** C4GPU.
**
** For the latest info, see https://github.com/c4gpu/c4gpu_runtime/
**
** Copyright (C) 2017 Wang Renxin. All rights reserved.
**
** The C# eval test.
*/
using System;
using System.Diagnostics;
using System.Threading.Tasks;
public class Program
{
public struct Vector2
{
public float x, y;
public Vector2(float _x, float _y)
{
x = _x; y = _y;
}
}
public struct Vector4
{
public float x, y, z, w;
public Vector4(float _x, float _y, float _z, float _w)
{
x = _x; y = _y; z = _z; w = _w;
}
}
private unsafe static float vec2_length(Vector2* p)
{
return (float)Math.Sqrt(p->x * p->x + p->y * p->y);
}
private unsafe static void vec2_mul_float(Vector2* p, float q)
{
p->x *= q;
p->y *= q;
}
private unsafe static void vec2_mul_vec2(Vector2* p, Vector2* q)
{
p->x *= q->x;
p->y *= q->y;
}
private unsafe static void vec4_add_vec4(Vector4* p, Vector4* q)
{
p->x += q->x;
p->y += q->y;
p->z += q->z;
p->w += q->w;
}
private unsafe static void vec4_mul_float(Vector4* p, float q)
{
p->x *= q;
p->y *= q;
p->z *= q;
p->w *= q;
}
private unsafe static void accel(Vector2* o, Vector2* p)
{
float l = 1.0f / vec2_length(p);
vec2_mul_float(o, -1 * (l * l * l));
}
private unsafe static void eval(Vector4* o, Vector4* u0, Vector4* v0)
{
Vector4 p = *u0;
Vector4 v = *v0;
const float dt = 0.1f;
for (int i = 0; i < 10000; ++i)
{
Vector2 a1, a2;
Vector2 m = new Vector2(p.x, p.y);
Vector2 n = new Vector2(p.z, p.w);
accel(&a1, &m);
accel(&a2, &n);
Vector4 a = new Vector4(a1.x, a1.y, a2.x, a2.y);
Vector4 _v = v;
vec4_mul_float(&_v, dt);
vec4_add_vec4(&p, &_v);
vec4_mul_float(&a, dt);
vec4_add_vec4(&v, &a);
}
*o = p;
}
public unsafe static void Main()
{
// Prepares.
Console.Out.WriteLine("Preparing...");
Vector4 un0 = new Vector4(100, 100, 100, 100);
Vector4[] in0 = new Vector4[1000000];
for (int i = 0; i < in0.Length; ++i)
{
in0[i] = new Vector4(1, 2, 3, 4);
}
Vector4[] out0 = new Vector4[in0.Length];
// Computes.
Console.Out.WriteLine("Begin compute...");
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < in0.Length; ++i)
{
fixed (Vector4* a = &out0[i], b = &in0[i])
{
eval(a, &un0, b);
}
}
sw.Stop();
Console.Out.WriteLine(string.Format("{0}ms cost.", sw.ElapsedMilliseconds));
// Done.
Console.Out.WriteLine("Done.");
Console.In.Read();
}
}