forked from NVIDIA/cuda-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMandelbrot.cpp
1272 lines (1048 loc) · 34.7 KB
/
Mandelbrot.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Mandelbrot sample
submitted by Mark Granger, NewTek
CUDA 2.0 SDK - updated with double precision support
CUDA 2.1 SDK - updated to demonstrate software block scheduling using
atomics
CUDA 2.2 SDK - updated with drawing of Julia sets by Konstantin Kolchin,
NVIDIA
*/
// OpenGL Graphics includes
#include <helper_gl.h>
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
#include <GL/wglew.h>
#endif
#if defined(__APPLE__) || defined(__MACOSX)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#include <GLUT/glut.h>
#ifndef glutCloseFunc
#define glutCloseFunc glutWMCloseFunc
#endif
#else
#include <GL/freeglut.h>
#endif
// CUDA runtime
// CUDA utilities and system includes
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <helper_functions.h>
#include <helper_cuda.h>
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include "Mandelbrot_kernel.h"
#include "Mandelbrot_gold.h"
#define MAX_EPSILON_ERROR 5.0f
// Define the files that are to be save and the reference images for validation
const char *sOriginal[] = {"mandelbrot.ppm", "julia.ppm", NULL};
const char *sReference[] = {"Mandelbrot_fp32.ppm", "Mandelbrot_fp64.ppm", NULL};
const char *sReferenceJulia[] = {"referenceJulia_fp32.ppm",
"referenceJulia_fp64.ppm", NULL};
bool g_isJuliaSet = false;
bool g_isMoving = true;
bool g_runCPU = false;
FILE *stream;
char g_ExecPath[300];
// Set to 1 to run on the CPU instead of the GPU for timing comparison.
#define RUN_CPU 0
// Set to 1 to time frame generation
#define RUN_TIMING 0
// Random number macros
#define RANDOMSEED(seed) ((seed) = ((seed)*1103515245 + 12345))
#define RANDOMBITS(seed, bits) ((unsigned int)RANDOMSEED(seed) >> (32 - (bits)))
// OpenGL PBO and texture "names"
GLuint gl_PBO, gl_Tex, gl_Shader;
struct cudaGraphicsResource *cuda_pbo_resource; // handles OpenGL-CUDA exchange
// Source image on the host side
uchar4 *h_Src = 0;
// Destination image on the GPU side
uchar4 *d_dst = NULL;
// Original image width and height
int imageW = 800, imageH = 600;
// Starting iteration limit
int crunch = 512;
// Starting position and scale
double xOff = -0.5;
double yOff = 0.0;
double scale = 3.2;
// Starting stationary position and scale motion
double xdOff = 0.0;
double ydOff = 0.0;
double dscale = 1.0;
// Julia parameter
double xJParam = 0.0;
double yJParam = 0.0;
// Precision mode
// 0=single precision, 1=double single, 2=double
int precisionMode = 0;
// Starting animation frame and anti-aliasing pass
int animationFrame = 0;
int animationStep = 0;
int pass = 0;
// Starting color multipliers and random seed
int colorSeed = 0;
uchar4 colors;
// Timer ID
StopWatchInterface *hTimer = NULL;
// User interface variables
int lastx = 0;
int lasty = 0;
bool leftClicked = false;
bool middleClicked = false;
bool rightClicked = false;
bool haveDoubles = true;
int numSMs = 0; // number of multiprocessors
int version = 1; // Compute Capability
// Auto-Verification Code
const int frameCheckNumber = 60;
int fpsCount = 0; // FPS count for averaging
int fpsLimit = 15; // FPS limit for sampling
unsigned int frameCount = 0;
unsigned int g_TotalErrors = 0;
int *pArgc = NULL;
char **pArgv = NULL;
const char *sSDKsample = "CUDA Mandelbrot/Julia Set";
#define MAX_EPSILON 50
#define REFRESH_DELAY 10 // ms
#ifndef MAX
#define MAX(a, b) ((a > b) ? a : b)
#endif
#define BUFFER_DATA(i) ((char *)0 + i)
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
// This is specifically to enable the application to enable/disable vsync
typedef BOOL(WINAPI *PFNWGLSWAPINTERVALFARPROC)(int);
void setVSync(int interval) {
if (WGL_EXT_swap_control) {
wglSwapIntervalEXT =
(PFNWGLSWAPINTERVALFARPROC)wglGetProcAddress("wglSwapIntervalEXT");
wglSwapIntervalEXT(interval);
}
}
#endif
void computeFPS() {
frameCount++;
fpsCount++;
if (fpsCount == fpsLimit) {
char fps[256];
float ifps = 1.f / (sdkGetAverageTimerValue(&hTimer) / 1000.f);
sprintf(fps, "<CUDA %s Set> %3.1f fps",
g_isJuliaSet ? "Julia" : "Mandelbrot", ifps);
glutSetWindowTitle(fps);
fpsCount = 0;
fpsLimit = (int)MAX(1.f, (float)ifps);
sdkResetTimer(&hTimer);
}
}
void startJulia(const char *path) {
g_isJuliaSet = true;
g_isMoving = false;
if ((path == NULL) || (stream = fopen(path, "r")) == NULL) {
printf(
"JuliaSet: params.txt could not be opened. Using default "
"parameters\n");
xOff = -0.085760;
yOff = 0.007040;
scale = 3.200000;
xJParam = -0.172400;
yJParam = -0.652693;
} else {
fseek(stream, 0L, SEEK_SET);
fscanf(stream, "%lf %lf %lf %lf %lf", &xOff, &yOff, &scale, &xJParam,
&yJParam);
fclose(stream);
}
xdOff = 0.0;
ydOff = 0.0;
dscale = 1.0;
pass = 0;
}
// Get a sub-pixel sample location
void GetSample(int sampleIndex, float &x, float &y) {
static const unsigned char pairData[128][2] = {
{64, 64}, {0, 0}, {1, 63}, {63, 1}, {96, 32}, {97, 95},
{36, 96}, {30, 31}, {95, 127}, {4, 97}, {33, 62}, {62, 33},
{31, 126}, {67, 99}, {99, 65}, {2, 34}, {81, 49}, {19, 80},
{113, 17}, {112, 112}, {80, 16}, {115, 81}, {46, 15}, {82, 79},
{48, 78}, {16, 14}, {49, 113}, {114, 48}, {45, 45}, {18, 47},
{20, 109}, {79, 115}, {65, 82}, {52, 94}, {15, 124}, {94, 111},
{61, 18}, {47, 30}, {83, 100}, {98, 50}, {110, 2}, {117, 98},
{50, 59}, {77, 35}, {3, 114}, {5, 77}, {17, 66}, {32, 13},
{127, 20}, {34, 76}, {35, 110}, {100, 12}, {116, 67}, {66, 46},
{14, 28}, {23, 93}, {102, 83}, {86, 61}, {44, 125}, {76, 3},
{109, 36}, {6, 51}, {75, 89}, {91, 21}, {60, 117}, {29, 43},
{119, 29}, {74, 70}, {126, 87}, {93, 75}, {71, 24}, {106, 102},
{108, 58}, {89, 9}, {103, 23}, {72, 56}, {120, 8}, {88, 40},
{11, 88}, {104, 120}, {57, 105}, {118, 122}, {53, 6}, {125, 44},
{43, 68}, {58, 73}, {24, 22}, {22, 5}, {40, 86}, {122, 108},
{87, 90}, {56, 42}, {70, 121}, {8, 7}, {37, 52}, {25, 55},
{69, 11}, {10, 106}, {12, 38}, {26, 69}, {27, 116}, {38, 25},
{59, 54}, {107, 72}, {121, 57}, {39, 37}, {73, 107}, {85, 123},
{28, 103}, {123, 74}, {55, 85}, {101, 41}, {42, 104}, {84, 27},
{111, 91}, {9, 19}, {21, 39}, {90, 53}, {41, 60}, {54, 26},
{92, 119}, {51, 71}, {124, 101}, {68, 92}, {78, 10}, {13, 118},
{7, 84}, {105, 4}};
x = (1.0f / 128.0f) * (0.5f + (float)pairData[sampleIndex][0]);
y = (1.0f / 128.0f) * (0.5f + (float)pairData[sampleIndex][1]);
} // GetSample
// render Mandelbrot image using CUDA or CPU
void renderImage(bool bUseOpenGL, bool fp64, int mode) {
#if RUN_TIMING
pass = 0;
#endif
if (pass < 128) {
if (g_runCPU) {
int startPass = pass;
float xs, ys;
sdkResetTimer(&hTimer);
if (bUseOpenGL) {
// DEPRECATED: checkCudaErrors(cudaGLMapBufferObject((void**)&d_dst,
// gl_PBO));
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_pbo_resource, 0));
size_t num_bytes;
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
(void **)&d_dst, &num_bytes, cuda_pbo_resource));
}
// Get the anti-alias sub-pixel sample location
GetSample(pass & 127, xs, ys);
// Get the pixel scale and offset
double s = scale / (double)imageW;
double x = (xs - (double)imageW * 0.5f) * s + xOff;
double y = (ys - (double)imageH * 0.5f) * s + yOff;
// Run the mandelbrot generator
// Use the adaptive sampling version when animating.
if (pass && !startPass) {
if (precisionMode)
RunMandelbrotDSGold1(h_Src, imageW, imageH, crunch, x, y, xJParam,
yJParam, s, colors, pass++, animationFrame,
g_isJuliaSet);
else
RunMandelbrotGold1(h_Src, imageW, imageH, crunch, (float)x, (float)y,
(float)xJParam, (float)yJParam, (float)s, colors,
pass++, animationFrame, g_isJuliaSet);
} else {
if (precisionMode)
RunMandelbrotDSGold0(h_Src, imageW, imageH, crunch, x, y, xJParam,
yJParam, s, colors, pass++, animationFrame,
g_isJuliaSet);
else
RunMandelbrotGold0(h_Src, imageW, imageH, crunch, (float)x, (float)y,
(float)xJParam, (float)yJParam, (float)s, colors,
pass++, animationFrame, g_isJuliaSet);
}
checkCudaErrors(cudaMemcpy(d_dst, h_Src, imageW * imageH * sizeof(uchar4),
cudaMemcpyHostToDevice));
if (bUseOpenGL) {
// DEPRECATED: checkCudaErrors(cudaGLUnmapBufferObject(gl_PBO));
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0));
}
#if RUN_TIMING
printf("CPU = %5.8f\n", 0.001f * sdkGetTimerValue(&hTimer));
#endif
} else { // this is the GPU Path
float timeEstimate;
int startPass = pass;
sdkResetTimer(&hTimer);
if (bUseOpenGL) {
// DEPRECATED: checkCudaErrors(cudaGLMapBufferObject((void**)&d_dst,
// gl_PBO));
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_pbo_resource, 0));
size_t num_bytes;
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
(void **)&d_dst, &num_bytes, cuda_pbo_resource));
}
// Render anti-aliasing passes until we run out time (60fps approximately)
do {
float xs, ys;
// Get the anti-alias sub-pixel sample location
GetSample(pass & 127, xs, ys);
// Get the pixel scale and offset
double s = scale / (float)imageW;
double x = (xs - (double)imageW * 0.5f) * s + xOff;
double y = (ys - (double)imageH * 0.5f) * s + yOff;
// Run the mandelbrot generator
// Use the adaptive sampling version when animating.
if (pass && !startPass)
RunMandelbrot1(d_dst, imageW, imageH, crunch, x, y, xJParam, yJParam,
s, colors, pass++, animationFrame, precisionMode,
numSMs, g_isJuliaSet, version);
else
RunMandelbrot0(d_dst, imageW, imageH, crunch, x, y, xJParam, yJParam,
s, colors, pass++, animationFrame, precisionMode,
numSMs, g_isJuliaSet, version);
// Estimate the total time of the frame if one more pass is rendered
timeEstimate =
0.1f * sdkGetTimerValue(&hTimer) *
((float)(pass + 1 - startPass) / (float)(pass - startPass));
} while ((pass < 128) && (timeEstimate < 1.0f / 60.0f) && !RUN_TIMING);
if (bUseOpenGL) {
// DEPRECATED: checkCudaErrors(cudaGLUnmapBufferObject(gl_PBO));
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0));
}
#if RUN_TIMING
printf("GPU = %5.8f\n", 0.001f * sdkGetTimerValue(&hTimer);
#endif
}
}
}
// OpenGL display function
void displayFunc(void) {
sdkStartTimer(&hTimer);
if ((xdOff != 0.0) || (ydOff != 0.0)) {
if (g_isMoving || !g_isJuliaSet) {
xOff += xdOff;
yOff += ydOff;
} else {
xJParam += xdOff;
yJParam += ydOff;
}
pass = 0;
}
if (dscale != 1.0) {
scale *= dscale;
pass = 0;
}
if (animationStep) {
animationFrame -= animationStep;
pass = 0;
}
// render the Mandelbrot image
renderImage(true, g_isJuliaSet, precisionMode);
// load texture from PBO
// glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, gl_PBO);
glBindTexture(GL_TEXTURE_2D, gl_Tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, imageW, imageH, GL_RGBA,
GL_UNSIGNED_BYTE, BUFFER_DATA(0));
// glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
// fragment program is required to display floating point texture
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, gl_Shader);
glEnable(GL_FRAGMENT_PROGRAM_ARB);
glDisable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_FRAGMENT_PROGRAM_ARB);
sdkStopTimer(&hTimer);
glutSwapBuffers();
computeFPS();
} // displayFunc
void cleanup() {
if (h_Src) {
free(h_Src);
h_Src = 0;
}
sdkStopTimer(&hTimer);
sdkDeleteTimer(&hTimer);
// DEPRECATED: checkCudaErrors(cudaGLUnregisterBufferObject(gl_PBO));
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_pbo_resource));
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glDeleteBuffers(1, &gl_PBO);
glDeleteTextures(1, &gl_Tex);
glDeleteProgramsARB(1, &gl_Shader);
}
void initMenus();
// OpenGL keyboard function
void keyboardFunc(unsigned char k, int, int) {
int seed;
switch (k) {
case '\033':
case 'q':
case 'Q':
printf("Shutting down...\n");
#if defined(__APPLE__) || defined(MACOSX)
exit(EXIT_SUCCESS);
#else
glutDestroyWindow(glutGetWindow());
return;
#endif
break;
case '?':
printf("xOff = %5.8f\n", xOff);
printf("yOff = %5.8f\n", yOff);
printf("scale = %e\n", scale);
printf("detail = %d\n", crunch);
printf("color = %d\n", colorSeed);
printf("xJParam = %5.8f\n", xJParam);
printf("yJParam = %5.8f\n", yJParam);
printf("\n");
break;
case 'e':
case 'E':
// Reset all values to their defaults
g_isJuliaSet = false;
g_isMoving = true;
g_runCPU = false;
printf(
"All parameters are reset to defaults. GPU implementation is "
"used.\n");
xOff = -0.5;
yOff = 0.0;
scale = 3.2;
xdOff = 0.0;
ydOff = 0.0;
dscale = 1.0;
colorSeed = 0;
colors.x = 3;
colors.y = 5;
colors.z = 7;
crunch = 512;
animationFrame = 0;
animationStep = 0;
xJParam = 0.0;
yJParam = 0.0;
pass = 0;
break;
case 'c':
seed = ++colorSeed;
if (seed) {
colors.x = RANDOMBITS(seed, 4);
colors.y = RANDOMBITS(seed, 4);
colors.z = RANDOMBITS(seed, 4);
} else {
colors.x = 3;
colors.y = 5;
colors.z = 7;
}
pass = 0;
break;
case 'C':
seed = --colorSeed;
if (seed) {
colors.x = RANDOMBITS(seed, 4);
colors.y = RANDOMBITS(seed, 4);
colors.z = RANDOMBITS(seed, 4);
} else {
colors.x = 3;
colors.y = 5;
colors.z = 7;
}
pass = 0;
break;
case 'a':
if (animationStep < 0) {
animationStep = 0;
} else {
animationStep++;
if (animationStep > 8) {
animationStep = 8;
}
}
break;
case 'A':
if (animationStep > 0) {
animationStep = 0;
} else {
animationStep--;
if (animationStep < -8) {
animationStep = -8;
}
}
break;
case 'd':
if (2 * crunch <= MIN(numSMs * (version < 20 ? 512 : 2048), 0x4000)) {
crunch *= 2;
pass = 0;
}
printf("detail = %d\n", crunch);
break;
case 'D':
if (crunch > 2) {
crunch /= 2;
pass = 0;
}
printf("detail = %d\n", crunch);
break;
case 'r':
colors.x -= 1;
pass = 0;
break;
case 'R':
colors.x += 1;
pass = 0;
break;
case 'g':
colors.y -= 1;
pass = 0;
break;
case 'G':
colors.y += 1;
pass = 0;
break;
case 'b':
colors.z -= 1;
pass = 0;
break;
case 'B':
colors.z += 1;
pass = 0;
break;
case 's':
case 'S':
if (g_runCPU) {
g_runCPU = false;
printf("GPU implementation\n");
} else {
g_runCPU = true;
printf("CPU implementation\n");
}
pass = 0;
glutDestroyMenu(glutGetMenu());
initMenus();
break;
case 'j':
case 'J':
// toggle between Mandelbrot and Julia sets and reset all parameters
if (!g_isJuliaSet) { // settings for Julia
g_isJuliaSet = true;
startJulia("params.txt");
} else { // settings for Mandelbrot
g_isJuliaSet = false;
g_isMoving = true;
xOff = -0.5;
yOff = 0.0;
scale = 3.2;
xdOff = 0.0;
ydOff = 0.0;
dscale = 1.0;
colorSeed = 0;
colors.x = 3;
colors.y = 5;
colors.z = 7;
crunch = 512;
animationFrame = 0;
animationStep = 0;
pass = 0;
}
char fps[30];
sprintf(fps, "<CUDA %s Set>", g_isJuliaSet ? "Julia" : "Mandelbrot");
glutSetWindowTitle(fps);
break;
case 'm':
case 'M':
if (g_isJuliaSet) {
g_isMoving = !g_isMoving;
pass = 0;
}
break;
case 'p':
case 'P':
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
if (fopen_s(&stream, "params.txt", "w") != 0)
#else
if ((stream = fopen("params.txt", "w")) == NULL)
#endif
{
printf("The file params.txt was not opened\n");
break;
}
fprintf(stream, "%f %f %f %f %f\n", xOff, yOff, scale, xJParam, yJParam);
fclose(stream);
break;
case 'o':
case 'O':
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
if (fopen_s(&stream, "params.txt", "r") != 0)
#else
if ((stream = fopen("params.txt", "r")) == NULL)
#endif
{
printf("The file params.txt was not opened\n");
break;
}
fseek(stream, 0L, SEEK_SET);
fscanf(stream, "%lf %lf %lf %lf %lf", &xOff, &yOff, &scale, &xJParam,
&yJParam);
xdOff = 0.0;
ydOff = 0.0;
dscale = 1.0;
fclose(stream);
pass = 0;
break;
case '4': // Left arrow key
xOff -= 0.05f * scale;
pass = 0;
break;
case '8': // Up arrow key
yOff += 0.05f * scale;
pass = 0;
break;
case '6': // Right arrow key
xOff += 0.05f * scale;
pass = 0;
break;
case '2': // Down arrow key
yOff -= 0.05f * scale;
pass = 0;
break;
case '+':
scale /= 1.1f;
pass = 0;
break;
case '-':
scale *= 1.1f;
pass = 0;
break;
default:
break;
}
} // keyboardFunc
// OpenGL mouse click function
void clickFunc(int button, int state, int x, int y) {
if (button == 0) {
leftClicked = !leftClicked;
}
if (button == 1) {
middleClicked = !middleClicked;
}
if (button == 2) {
rightClicked = !rightClicked;
}
int modifiers = glutGetModifiers();
if (leftClicked && (modifiers & GLUT_ACTIVE_SHIFT)) {
leftClicked = 0;
middleClicked = 1;
}
if (state == GLUT_UP) {
leftClicked = 0;
middleClicked = 0;
}
lastx = x;
lasty = y;
xdOff = 0.0;
ydOff = 0.0;
dscale = 1.0;
} // clickFunc
// OpenGL mouse motion function
void motionFunc(int x, int y) {
double fx = (double)(x - lastx) / 50.0 / (double)(imageW);
double fy = (double)(lasty - y) / 50.0 / (double)(imageH);
if (leftClicked) {
xdOff = fx * scale;
ydOff = fy * scale;
} else {
xdOff = 0.0f;
ydOff = 0.0f;
}
if (middleClicked)
if (fy > 0.0f) {
dscale = 1.0 - fy;
dscale = dscale < 1.05 ? dscale : 1.05;
} else {
dscale = 1.0 / (1.0 + fy);
dscale = dscale > (1.0 / 1.05) ? dscale : (1.0 / 1.05);
}
else {
dscale = 1.0;
}
} // motionFunc
void timerEvent(int value) {
if (glutGetWindow()) {
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
}
}
void mainMenu(int i) {
precisionMode = i;
pass = 0;
}
void initMenus() {
glutCreateMenu(mainMenu);
if (!g_runCPU) {
glutAddMenuEntry("Hardware single precision", 0);
if (numSMs > 2) {
glutAddMenuEntry("Emulated double-single precision", 1);
}
if (haveDoubles) {
glutAddMenuEntry("Hardware double precision", 2);
}
} else {
glutAddMenuEntry("Software single precision", 0);
glutAddMenuEntry("Software double precision", 1);
}
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
// gl_Shader for displaying floating-point texture
static const char *shader_code =
"!!ARBfp1.0\n"
"TEX result.color, fragment.texcoord, texture[0], 2D; \n"
"END";
GLuint compileASMShader(GLenum program_type, const char *code) {
GLuint program_id;
glGenProgramsARB(1, &program_id);
glBindProgramARB(program_type, program_id);
glProgramStringARB(program_type, GL_PROGRAM_FORMAT_ASCII_ARB,
(GLsizei)strlen(code), (GLubyte *)code);
GLint error_pos;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
if (error_pos != -1) {
const GLubyte *error_string;
error_string = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
fprintf(stderr, "Program error at position: %d\n%s\n", (int)error_pos,
error_string);
return 0;
}
return program_id;
}
void initOpenGLBuffers(int w, int h) {
// delete old buffers
if (h_Src) {
free(h_Src);
h_Src = 0;
}
if (gl_Tex) {
glDeleteTextures(1, &gl_Tex);
gl_Tex = 0;
}
if (gl_PBO) {
// DEPRECATED: checkCudaErrors(cudaGLUnregisterBufferObject(gl_PBO));
cudaGraphicsUnregisterResource(cuda_pbo_resource);
glDeleteBuffers(1, &gl_PBO);
gl_PBO = 0;
}
// allocate new buffers
h_Src = (uchar4 *)malloc(w * h * 4);
printf("Creating GL texture...\n");
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &gl_Tex);
glBindTexture(GL_TEXTURE_2D, gl_Tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
h_Src);
printf("Texture created.\n");
printf("Creating PBO...\n");
glGenBuffers(1, &gl_PBO);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, gl_PBO);
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, w * h * 4, h_Src, GL_STREAM_COPY);
// While a PBO is registered to CUDA, it can't be used
// as the destination for OpenGL drawing calls.
// But in our particular case OpenGL is only used
// to display the content of the PBO, specified by CUDA kernels,
// so we need to register/unregister it only once.
// DEPRECATED: checkCudaErrors( cudaGLRegisterBufferObject(gl_PBO) );
checkCudaErrors(cudaGraphicsGLRegisterBuffer(
&cuda_pbo_resource, gl_PBO, cudaGraphicsMapFlagsWriteDiscard));
printf("PBO created.\n");
// load shader program
gl_Shader = compileASMShader(GL_FRAGMENT_PROGRAM_ARB, shader_code);
}
void reshapeFunc(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
if (w != 0 && h != 0) // Do not call when window is minimized that is when
// width && height == 0
initOpenGLBuffers(w, h);
imageW = w;
imageH = h;
pass = 0;
glutPostRedisplay();
}
void initGL(int *argc, char **argv) {
printf("Initializing GLUT...\n");
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(imageW, imageH);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
glutDisplayFunc(displayFunc);
glutKeyboardFunc(keyboardFunc);
glutMouseFunc(clickFunc);
glutMotionFunc(motionFunc);
glutReshapeFunc(reshapeFunc);
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
initMenus();
if (!isGLVersionSupported(1, 5) ||
!areGLExtensionsSupported(
"GL_ARB_vertex_buffer_object GL_ARB_pixel_buffer_object")) {
fprintf(stderr, "Error: failed to get minimal extensions for demo\n");
fprintf(stderr, "This sample requires:\n");
fprintf(stderr, " OpenGL version 1.5\n");
fprintf(stderr, " GL_ARB_vertex_buffer_object\n");
fprintf(stderr, " GL_ARB_pixel_buffer_object\n");
exit(EXIT_SUCCESS);
}
printf("OpenGL window created.\n");
}
void initData(int argc, char **argv) {
// check for hardware double precision support
int dev = 0;
dev = findCudaDevice(argc, (const char **)argv);
cudaDeviceProp deviceProp;
checkCudaErrors(cudaGetDeviceProperties(&deviceProp, dev));
version = deviceProp.major * 10 + deviceProp.minor;
numSMs = deviceProp.multiProcessorCount;
// initialize some of the arguments
if (checkCmdLineFlag(argc, (const char **)argv, "xOff")) {
xOff = getCmdLineArgumentFloat(argc, (const char **)argv, "xOff");
}
if (checkCmdLineFlag(argc, (const char **)argv, "yOff")) {
yOff = getCmdLineArgumentFloat(argc, (const char **)argv, "yOff");
}
if (checkCmdLineFlag(argc, (const char **)argv, "scale")) {
scale = getCmdLineArgumentFloat(argc, (const char **)argv, "xOff");
}
colors.w = 0;