-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from HamletTanyavong/dev
Add support for complex numbers in methods that use OpenCL
- Loading branch information
Showing
9 changed files
with
206 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
__kernel void comp_mat_mul(__global const complex* matA, | ||
__global const complex* matB, | ||
int const k, | ||
__global complex* result) | ||
{ | ||
int row = get_global_id(0); | ||
int col = get_global_id(1); | ||
int width = get_local_size(1) * get_num_groups(1); | ||
|
||
int aIndex = row * k; | ||
int bIndex = col; | ||
|
||
complex sum; | ||
sum.re = 0; | ||
sum.im = 0; | ||
|
||
for (int i = 0; i < k; i++) | ||
{ | ||
sum = comp_add(sum, comp_mul(matA[aIndex], matB[bIndex])); | ||
aIndex++; | ||
bIndex += width; | ||
} | ||
|
||
result[row * width + col] = sum; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Mathematics.NET/GPU/OpenCL/Kernels/comp_vec_mul_scalar.cl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__kernel void comp_vec_mul_scalar(__global const complex* vector, | ||
const complex scalar, | ||
__global complex* result) | ||
{ | ||
int i = get_global_id(0); | ||
result[i] = comp_mul(vector[i], scalar); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
typedef struct __attribute__((packed)) { | ||
double re; | ||
double im; | ||
} complex; | ||
|
||
inline complex comp_add(const complex z, const complex w) { | ||
complex result; | ||
result.re = z.re + w.re; | ||
result.im = z.im + w.im; | ||
return result; | ||
} | ||
|
||
inline complex comp_div(const complex z, const complex w) { | ||
double a = z.re; | ||
double b = z.im; | ||
double c = w.re; | ||
double d = w.im; | ||
|
||
complex result; | ||
if (fabs(d) < fabs(c)) { | ||
double u = d / c; | ||
result.re = (a + b * u) / (c + d * u); | ||
result.im = (b - a * u) / (c + d * u); | ||
} else { | ||
double u = c / d; | ||
result.re = (b + a * u) / (d + c * u); | ||
result.im = (b * u - a) / (d + c * u); | ||
} | ||
return result; | ||
} | ||
|
||
inline complex comp_mul(const complex z, const complex w) { | ||
complex result; | ||
result.re = z.re * w.re - z.im * w.im; | ||
result.im = z.re * w.im + w.re * z.im; | ||
return result; | ||
} | ||
|
||
inline complex comp_sub(const complex z, const complex w) { | ||
complex result; | ||
result.re = z.re - w.re; | ||
result.im = z.im - w.im; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters