-
Notifications
You must be signed in to change notification settings - Fork 0
/
cudatest.cu
71 lines (58 loc) · 1.75 KB
/
cudatest.cu
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
//
// Created by af on 25.01.21.
//
#include <iostream>
#define AML_CUDA
#include "amathlib.h"
__global__ void kernel(double width, double height, int accuracy, int *results) {
int i = blockIdx.x * blockDim.x + threadIdx.x; //width
if (i >= (int) width) {
return;
}
int j = blockIdx.y * blockDim.y + threadIdx.y; //height
if (j >= (int) height) {
return;
}
results[i + j * (int) width] = accuracy;
results[i + j * (int) width] = 0;
if (!(i >= width || j >= height)) {
CU_Complex64 c = {CU_AML::mapLinear((double) j, 0.0, (double) height, -1.5, 0.5),
CU_AML::mapLinear((double) i, 0.0, (double) width, -1.0, 1.0)};
CU_Complex64 z = c;
for (int x = 0; x < accuracy; ++x) {
z = z * z + c;
if (z.abs_gt(2)) {
results[i + j * (int) width] = accuracy;
break;
}
}
}
}
int main() {
const int width = 250;
const int height = 80;
const int accuracy = 100000;
int *deviceResults;
cudaMalloc(&deviceResults, width * height * sizeof(int));
dim3 threadsPerBlock(16, 16);
dim3 numBlocks((width + threadsPerBlock.x - 1) / threadsPerBlock.x,
(height + threadsPerBlock.y - 1) / threadsPerBlock.y);
kernel<<< numBlocks, threadsPerBlock>>>(width, height, accuracy, deviceResults);
std::cout << " : " << width << " : " << std::endl;
int *results = (int *) malloc(width * height * sizeof(int));
cudaMemcpy(results, deviceResults, width * height * sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(deviceResults);
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
if (results[y + x * (int) width] >= accuracy) {
std::cout << " ";
} else if (results[y + x * (int) width] == 10) UNLIKELY {
std::cout << ".";
} else LIKELY {
std::cout << "#";
}
}
std::cout << "\n";
}
free(results);
}