Changes

Jump to: navigation, search

Top Solo

7,414 bytes added, 03:31, 8 March 2013
Issues Encountered
I am going to try and profile and optimize the following function from the CImg Library.
This function creates triangles on the screen and allows the user to make them clump up by clicking the mouse buttons.
 
This function, like others in the CImg_demo.cpp library uses the <CImg.h> library. This is a massive library that contains functions and routines responsible for displaying and preparing the environment for Image and Video processing. My function 'Filled Triangles' uses this library and its resources extensively.
Compilation with profiling on Mac OSX:
 
 
'''g++ -o CImg_demo CImg_demo.cpp -O2 -g -pg -I.. -Wall -W -ansi -pedantic -Dcimg_use_vt100 -I/usr/X11R6/include -lm -L/usr/X11R6/lib -lpthread -lX11'''
=== The Program - Filled Triangles ===
<big><presource lang="cpp">
// Item : Filled Triangles
// Init images and create display window.
 
CImg<unsigned char> img0(background), img;
CImgDisplay disp(img0,"[#6] - Filled Triangles (Click to shrink)");
 
 
// Define random properties (pos, size, colors, ..) for all triangles that will be displayed.
 
float posx[SIZE], posy[SIZE], rayon[SIZE], angle[SIZE], veloc[SIZE], opacity[SIZE];
std::srand((unsigned int)time(0));
// I'm thinking of offloading these operations to the GPU
 
for (int k = 0; k<SIZE; ++k) {
opacity[k] = (float)(0.3 + 1.5*cimg::rand());
 
}
 
 
// Start animation loop.
while (!disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC()) {
 
img = img0;
 
 
/* Maybe offload this for loop to GPU? */
}
 
 
// Display current animation framerate, and refresh display window.
if (++num>SIZE) num = SIZE;
 
 
// Allow the user to toggle fullscreen mode, by pressing CTRL+F.
}
 </pre></bigsource>
=== Initial Profile (on OpenSUSE Linux at Seneca) ===
<big><pre>
Flat profile:
 
 
 
Each sample counts as 0.01 seconds.
0.00 3.10 0.00 1 0.00 136.13 cimg_library::CImgList<float>::get_crop_font() const
</pre></big>
=== Modified Makefile for Profiling ===
<bigsource lang="bash"><pre> 
CIMG_FILES = CImg_demo
 
 
# Files which requires external libraries to run.
gmic
 
 
#---------------------------------
 
# Set correct variables and paths
 
#---------------------------------
CIMG_VERSION = 1.5.4
endif
 
 
#--------------------------------------------------
 
# Set compilation flags allowing to customize CImg
 
#--------------------------------------------------
 
# Flags to enable strict code standards
endif
 
 
# Flags to enable code debugging.
endif
 
 
#-------------------------
 
# Define Makefile entries
 
#-------------------------
.cpp:
endif
 
menu:
 
@echo
 
@echo "CImg Library $(CIMG_VERSION) : Examples"
 
@echo "-----------------------------"
 
@echo " > linux : Linux/BSD target, X11 display, optimizations disabled."
 
@echo " > dlinux : Linux/BSD target, X11 display, debug mode."
 
@echo " > olinux : Linux/BSD target, X11 display, optimizations enabled."
 
@echo " > mlinux : Linus/BSD target, no display, minimal features, optimizations enabled."
 
@echo " > Mlinux : Linux/BSD target, X11 display, maximal features, optimizations enabled."
 
@echo
 
@echo " > solaris : Sun Solaris target, X11 display, optimizations disabled."
 
@echo " > dsolaris : Sun Solaris target, X11 display, debug mode."
 
@echo " > osolaris : Sun Solaris target, X11 display, optimizations enabled."
 
@echo " > msolaris : Sun Solaris target, no display, minimal features, optimizations enabled."
 
@echo " > Msolaris : Sun Solaris target, X11 display, maximal features, optimizations enabled."
 
@echo
 
@echo " > macosx : MacOSX target, X11 display, optimizations disabled."
 
@echo " > dmacosx : MacOSX target, X11 display, debug mode."
 
@echo " > omacosx : MacOSX target, X11 display, optimizations enabled."
 
@echo " > mmacosx : MacOSX target, no display, minimal features, optimizations enabled."
 
@echo " > Mmacosx : MacOSX target, X11 display, maximal features, optimizations enabled."
 
@echo
 
@echo " > windows : Windows target, GDI32 display, optimizations disabled."
 
@echo " > dwindows : Windows target, GDI32 display, debug mode."
 
@echo " > owindows : Windows target, GDI32 display, optimizations enabled."
 
@echo " > mwindows : Windows target, no display, minimal features, optimizations enabled."
 
@echo " > Mwindows : Windows target, GDI32 display, maximal features, optimizations enabled."
 
@echo
 
@echo " > clean : Clean generated files."
 
@echo
 
@echo "Choose your option :"
 
@read CHOICE; echo; $(MAKE) $$CHOICE; echo; echo "> Next time, you can bypass the menu by typing directly 'make $$CHOICE'"; echo;
 
 
all: $(CIMG_FILES)
 
clean:
endif
 
 
# Specific targets for the 'gmic_gimp' plug-in for GIMP.
 
gmic_gimp.o: gmic.cpp gmic_def.h
 
$(CC) -o gmic_gimp.o -c gmic.cpp -Dcimg_build -Dgmic_gimp -Dgmic_float_only $(CIMG_OPT_CFLAGS) $(CIMG_FFTW3_CFLAGS) $(CIMG_PNG_CFLAGS)
 
 
gmic_gimp: gmic_gimp.o gmic_gimp.cpp
 
$(CC) -o $(EXEPFX)gmic_gimp gmic_gimp.cpp gmic_gimp.o -Dcimg_build `gimptool-2.0 --cflags` `gimptool-2.0 --libs` $(CIMG_GIMP_CFLAGS) $(CIMG_OPT_FLAGS) $(CIMG_FFTW3_LDFLAGS) $(CIMG_PNG_LDFLAGS) -lpthread
 
 
 
 
# Specific targets for 'gmic'.
 
gmic_def: gmic_def.gmic
 
cd ../../gmic/src && $(MAKE) def
 
 
 
 
gmic_bool.o: gmic.cpp
 
@echo
 
@echo "** Compiling 'gmic ($(CIMG_VERSION))' with '$(CCVER)'"
 
@echo
 
$(CC) -o gmic_bool.o -c gmic.cpp -Dcimg_build -Dgmic_split_compilation -Dgmic_bool $(CFLAGS) $(CONF_CFLAGS)
 
gmic_uchar.o: gmic.cpp
 
$(CC) -o gmic_uchar.o -c gmic.cpp -Dcimg_build -Dgmic_split_compilation -Dgmic_uchar $(CFLAGS) $(CONF_CFLAGS)
 
gmic_char.o: gmic.cpp
 
$(CC) -o gmic_char.o -c gmic.cpp -Dcimg_build -Dgmic_split_compilation -Dgmic_char $(CFLAGS) $(CONF_CFLAGS)
 
gmic_ushort.o: gmic.cpp
 
$(CC) -o gmic_ushort.o -c gmic.cpp -Dcimg_build -Dgmic_split_compilation -Dgmic_ushort $(CFLAGS) $(CONF_CFLAGS)
 
gmic_short.o: gmic.cpp
 
$(CC) -o gmic_short.o -c gmic.cpp -Dcimg_build -Dgmic_split_compilation -Dgmic_short $(CFLAGS) $(CONF_CFLAGS)
 
gmic_uint.o: gmic.cpp
 
$(CC) -o gmic_uint.o -c gmic.cpp -Dcimg_build -Dgmic_split_compilation -Dgmic_uint $(CFLAGS) $(CONF_CFLAGS)
 
gmic_int.o: gmic.cpp
 
$(CC) -o gmic_int.o -c gmic.cpp -Dcimg_build -Dgmic_split_compilation -Dgmic_int $(CFLAGS) $(CONF_CFLAGS)
 
gmic_float.o: gmic.cpp
 
$(CC) -o gmic_float.o -c gmic.cpp -Dgmic_main -Dcimg_build -Dgmic_split_compilation -Dgmic_float $(CFLAGS) $(CONF_CFLAGS)
 
gmic_double.o: gmic.cpp
 
$(CC) -o gmic_double.o -c gmic.cpp -Dcimg_build -Dgmic_split_compilation -Dgmic_double $(CFLAGS) $(CONF_CFLAGS)
 
 
gmic: gmic_bool.o gmic_uchar.o gmic_char.o gmic_ushort.o gmic_short.o gmic_uint.o gmic_int.o gmic_float.o gmic_double.o gmic_def.h
 
$(CC) -o $(EXEPFX)gmic -Dcimg_build -Dgmic_split_compilation \
 
gmic_bool.o gmic_uchar.o gmic_char.o gmic_ushort.o gmic_short.o \
 
gmic_uint.o gmic_int.o gmic_float.o gmic_double.o $(CFLAGS) $(CONF_CFLAGS) $(CONF_LDFLAGS) $(LDFLAGS)
 
 
gmic_minimal:
 
$(CC) -o $(EXEPFX)gmic gmic.cpp -Dgmic_main -Dgmic_float_only -Dcimg_build -Dcimg_use_fftw3 -Dcimg_use_png -Dcimg_display=1 -I/usr/X11R6/include -g -W -Wall -ansi -pedantic -L/usr/X11R6/lib -lX11 -lpthread -lfftw3 -lpng -lz
 
 
ifeq ($(STRIP_EXE),true)
 
strip $(EXEPFX)gmic
 
endif
 
# Linux/BSD/Mac OSX targets, with X11 display.
all
# MacOsX targets, with X11 display.
dlinuxmacosx:
@$(MAKE) \
"CONF_CFLAGS = \
 
$(PROFILE) \
 
$(PROFILE2) \
$(CIMG_ANSI_CFLAGS) \
$(CIMG_DEBUG_CFLAGSCIMG_VT100_CFLAGS) \ $(CIMG_X11_CFLAGS)" \ "CONF_LDFLAGS = \ $(CIMG_X11_LDFLAGS)" \ all # Windows targets, with GDI32 display.
$(CIMG_VT100_CFLAGS) \windows:
@$(CIMG_X11_CFLAGSMAKE) \
$(CIMG_XSHM_CFLAGS)"CONF_CFLAGS = " \
"CONF_LDFLAGS = \
$(CIMG_X11_LDFLAGSCIMG_GDI32_LDFLAGS) " \all</source> === Makefile changes ===
$(CIMG_XSHM_LDFLAGS)" \
allI have made changes to the original Makefile that was provided with this library. I have customized it to compile for profiling by default. A new build can be generate by cleaning the old build (eg: make clean). The source can be compiled by running make <platform> eg: macosx, linux. You will need to download a third party program to allow you to run the 'make' command on Windows but the execution is the same. it can also be run on windows using visual Studio or on the command line("cl.exe must be in the environnent PATH variable").
=== Build Instructions ===
olinux:This Makefile and application is cross-platform.
@$(MAKE) \make <platform> eg: macosx, linux etc. I have successfully built this application on the following platforms: Windows 7, OpenSuse linux and Mac OSX Lion.
"CONF_CFLAGS = \
$(CIMG_ANSI_CFLAGS) \=== Summary of Findings ===
$The execution of the program takes roughly 3.10 - 20 seconds (CIMG_OPT_CFLAGSdepending on how long you are measuring the calculations of triangle animations) \. it should be noted that this application initially was an application that relied upon user input for execution and for termination. I have modified this initial behavior by ensuring the while loop (which generates the triangles) executes only for a maximum of 1000 iterations. The time measured in this assignment is for every 1000 iterations of this loop.
$(CIMG_VT100_CFLAGS) \
$(CIMG_X11_CFLAGS) \=== Profiling Results ===The results if the initial profile shows that the execution time is most greatly consumed when drawing the triangles out to the screen one at a time. It seems like this can be optimized by offloading this drawing to n threads based on n triangles to be drawn. But this is subject to change because of any additional complexity that may be introduced that may include interoperability with both the GPU and CPU.
$There is another for loop which sets the dimensions for each triangle one by one in linear time O(CIMG_XSHM_CFLAGSn )" \. This process can also be out-sourced to the GPU in n threads for n triangles. I would need to determine if this process also involves interoperability between the CPU and GPU.
"CONF_LDFLAGS = \The complexity of the entire program is O(n^3). There is a for loop for setup, a while loop for accepting user input and another for loop for drawing the triangles.
$(CIMG_X11_LDFLAGS) \Also the times recorded can be increase if the maximum loop iterations increase ie: 10000,100000,1000000. This will identify the same relationship but with higher task time.
$(CIMG_XSHM_LDFLAGS)" \
"STRIP_EXE=true" \== Potential Parallelization Candidates ===
allUpon analyzing this function I discovered two possible areas where I could optimize the code using threads sent to the GPU. The first is a for loop which sets the attributes for 100 triangles in serial. This task can be done in parallel using 100 threads on the GPU.<source lang="cpp">
mlinux:for (int k = 0; k<100; ++k) {
@$ posx[k] = (MAKEfloat) \(cimg::rand()*img0.width());
"CONF_CFLAGS posy[k] = \(float)(cimg::rand()*img0.height());
$ rayon[k] = (CIMG_ANSI_CFLAGSfloat) \(10 + cimg::rand()*50);
$ angle[k] = (CIMG_NODISPLAY_CFLAGSfloat) \(cimg::rand()*360);
$ veloc[k] = (CIMG_OPT_CFLAGSfloat)" \(cimg::rand()*20 - 10);
"STRIP_EXE color[k][0] =true" \(unsigned char)(cimg::rand()*255);
all color[k][1] = (unsigned char)(cimg::rand()*255);
Mlinux color[k][2] = (unsigned char)(cimg::rand()*255);
@$ opacity[k] = (MAKEfloat) \(0.3 + 1.5*cimg::rand());
"CONF_CFLAGS = \ }
$(CIMG_OPT_CFLAGS) \</pre>
$(CIMG_VT100_CFLAGS) \
$(CIMG_X11_CFLAGS) \
$(CIMG_XSHM_CFLAGS) \The second instance where this is possible is a bit tricky. It involves another serial for loop. The purpose of this loop is to draw each of the triangles on the screen and manipulate them later on. I am not 100 percent sure this can be done in parallel in practice but in theory it should be possible because the application is drawing out each triangle one by one.
$(CIMG_XRANDR_CFLAGS) \<pre> // Draw each triangle on the background image.
$ for (CIMG_TIFF_CFLAGSint k = 0; k<num; ++k) \{
$(CIMG_MINC2_CFLAGS) \ const int
$ x0 = (CIMG_EXR_CFLAGSint) \(posx[k] + rayon[k]*std::cos(angle[k]*cimg::PI/180)),
$ y0 = (CIMG_PNG_CFLAGSint) \(posy[k] + rayon[k]*std::sin(angle[k]*cimg::PI/180)),
$ x1 = (CIMG_JPEG_CFLAGSint) \(posx[k] + rayon[k]*std::cos((angle[k] + 120)*cimg::PI/180)),
$ y1 = (CIMG_ZLIB_CFLAGSint) \(posy[k] + rayon[k]*std::sin((angle[k] + 120)*cimg::PI/180)),
$ x2 = (CIMG_OPENCV_CFLAGSint) \(posx[k] + rayon[k]*std::cos((angle[k] + 240)*cimg::PI/180)),
$ y2 = (CIMG_MAGICK_CFLAGSint) \(posy[k] + rayon[k]*std::sin((angle[k] + 240)*cimg::PI/180));
$ if (CIMG_FFTW3_CFLAGSk%10)" \img.draw_triangle(x0,y0,x1,y1,x2,y2,color[k],opacity[k]);
"CONF_LDFLAGS = \ else img.draw_triangle(x0,y0,x1,y1,x2,y2,img0,0,0,img0.width()-1,0,0,img.height()-1,opacity[k]);
$ img.draw_triangle(CIMG_X11_LDFLAGSx0,y0,x1,y1,x2,y2,white,opacity[k],~0U) \;
$(CIMG_XSHM_LDFLAGS) \
$(CIMG_XRANDR_LDFLAGS) \ // Make the triangles rotate, and check for mouse click event.
$ // (CIMG_TIFF_LDFLAGSto make triangles collapse or join) \.
$(CIMG_MINC2_LDFLAGS) \ angle[k]+=veloc[k];
$ if (CIMG_EXR_LDFLAGSdisp.mouse_x()>0 && disp.mouse_y()>0) \{
$ float u = disp.mouse_x(CIMG_PNG_LDFLAGS) \- posx[k], v = disp.mouse_y() - posy[k];
$ if (CIMG_JPEG_LDFLAGSdisp.button()) \{ u = -u; v = -v; }
$(CIMG_ZLIB_LDFLAGS) \ posx[k]-=0.03f*u, posy[k]-=0.03f*v;
$ if (CIMG_OPENCV_LDFLAGSposx[k]<0 || posx[k]>=img.width()) posx[k] = (float)(cimg::rand()*img.width()) \;
$ if (CIMG_MAGICK_LDFLAGSposy[k]<0 || posy[k]>=img.height()) posy[k] = (float)(cimg::rand()*img.height()) \;
$(CIMG_FFTW3_LDFLAGS)" \ }
"STRIP_EXE=true" \ }
all $(CIMG_EXTRA_FILES)
</source>
# Sun Solaris targets, with X11 display.=== Amdahls Law Calculations ===
solarisSince there are 100 Triangles generated then we can theoretically create 100 threads for each triangle. The draw_line, draw_triangle, and draw_image functions take up 16 percent(0.38 + 0.08 + 0.06 / 3.10) of the execution time of the application in seconds. Plugging that into the equation using 100 cores we get:
@$(MAKE) \S100 = 1/ 1 - 0.16 + 0.16 / 100
"CONF_CFLAGS = \1.18 or 1.2 speedup is theoretically achievable rounded up PER 1000 iterations of the while loop to draw these triangles.
$(CIMG_ANSI_CFLAGS) \Will I work on this Project?If I can optimize this function or any other function within the CImg library I will continue with this project. If it is not possible to optimize this project within the given time of the course then it will be difficult to continue on with this project and I will have to work with someone else's project. But my initial plan is to continue with this project unless I am told otherwise.
$(CIMG_VT100_CFLAGS) \=== Issues Encountered ===
$(CIMG_X11_CFLAGS) \'''Gprof on MacOSx with Intel processor'''
$(CIMG_XSHM_CFLAGS)" \
"CONF_LDFLAGS = \The profiling tool gprog does not work on the macbooks with an Intel processor installed (I have Intel Core i5). This was verified by numerous internet resources and annoying personal experience.
$(CIMG_SOLARIS_LDFLAGS) \
$(CIMG_X11_LDFLAGS) \'''Makefile builds EVERYTHING'''
$(CIMG_XSHM_LDFLAGS)" \The Makefile shipped with this open source library is programmed to build all of the modules located in CImg_demo.cpp. I had to modify the Makefile to only build the specified function within the library of Image processing functions
all== Assignment 2 ==
This assignment involved GPU programming the functions I had speculated I could process concurrently. After further analysis I determined that the second for loop cannot be processed in parallel without converting some underlying API functions to device functions. These functions are used in multiple areas in the framework so I did not pursue changing the framework too much. The first for loop however contained independent data that could be executed in parallel so I went ahead and created kernels for that code. I also noticed that the function made a call to cimg::rand(). This is obviously a function that returns random numbers. Luckily CUDA also has multiple libraries that perform these functions. Off the top of my head: Thrust and CURAND do. I went with CURAND because it has the word "rand" in it and that's what I need. hehe
=== Kernels ===
<source lang="cpp">
dsolaris:
@$(MAKE) \/*
"CONF_CFLAGS = \ * Setup and initialize curand with a seed
$(CIMG_ANSI_CFLAGS) \ */
$__global__ void initCurand(CIMG_DEBUG_CFLAGScurandState* state) \{
$(CIMG_VT100_CFLAGS) \ int idx = blockIdx.x * blockDim.x + threadIdx.x;
$ curand_init(CIMG_X11_CFLAGS100, idx, 0, &state[idx]) \;
$ __syncthreads(CIMG_XSHM_CFLAGS)" \;
"CONF_LDFLAGS = \}
$(CIMG_SOLARIS_LDFLAGS) \
$(CIMG_X11_LDFLAGS) \
$(CIMG_XSHM_LDFLAGS)" \/*
all * CUDA kernel that will execute 100 threads in parallel
* and will populate these parallel arrays with 100 random numbers
osolaris: * array size = 100.
@$(MAKE) \*/
"CONF_CFLAGS = \
$(CIMG_ANSI_CFLAGS) \
$__global__ void initializeArrays(CIMG_OPT_CFLAGS) \float* posx, float* posy,float* rayon, float* veloc,
$(CIMG_VT100_CFLAGS) \ float* opacity ,float* angle, unsigned char* color, int height,
$(CIMG_X11_CFLAGS int width, curandState* state, size_t pitch) \{
$(CIMG_XSHM_CFLAGS)" \
"CONF_LDFLAGS = \
$(CIMG_SOLARIS_LDFLAGS) \ int idx = blockIdx.x * blockDim.x + threadIdx.x;
$(CIMG_X11_LDFLAGS) \ curandState localState = state[idx];
$(CIMG_XSHM_LDFLAGS)" \
"STRIP_EXE=true" \
all posx[idx] = (float)(curand_normal(&localState)*width);
posy[idx] = (float)(curand_normal(&localState)*height);
msolaris: rayon[idx] = (float)(10 + curand_normal(&localState)*50);
@$ angle[idx] = (MAKEfloat) \(curand_normal(&localState)*360);
"CONF_CFLAGS veloc[idx] = \(float)(curand_uniform(&localState)*20 - 10);
$ color[idx*pitch] = (CIMG_ANSI_CFLAGSunsigned char) \(curand_normal(&localState)*255);
$ color[(CIMG_NODISPLAY_CFLAGSidx*pitch) \+1] = (unsigned char)(curand_normal(&localState)*255);
$ color[(CIMG_OPT_CFLAGSidx*pitch)" \+2] = (unsigned char)(curand_normal(&localState)*255);
"STRIP_EXE opacity[idx] =true" \(float)(0.3f + 1.5f *curand_normal(&localState));
all
__syncthreads();
}
// check for any errors returned by CUDA API functions.
Msolaris:void errCheck(cudaError_t err, const char* msg){
@$ if (MAKEerr != cudaSuccess) \
std::cout<< msg << "CONF_CFLAGS = \: " << cudaGetErrorString(err) << std::endl;
$(CIMG_OPT_CFLAGS) \}
$(CIMG_VT100_CFLAGS) \</source>
$(CIMG_X11_CFLAGS) \=== Body Code ===<source lang="cpp">
$(CIMG_XSHM_CFLAGS) \
$(CIMG_XRANDR_CFLAGS) \// check for any errors returned by CUDA API functions.
$void errCheck(CIMG_TIFF_CFLAGScudaError_t err, const char* msg) \{
$ if (CIMG_MINC2_CFLAGSerr != cudaSuccess) \
$ std::cout<< msg << ": " << cudaGetErrorString(CIMG_EXR_CFLAGSerr) \<< std::endl;
$(CIMG_PNG_CFLAGS) \}
$(CIMG_JPEG_CFLAGS) \
$(CIMG_ZLIB_CFLAGS) \
$(CIMG_OPENCV_CFLAGS) \/*---------------------------
$(CIMG_MAGICK_CFLAGS) \
$(CIMG_FFTW3_CFLAGS)" \
"CONF_LDFLAGS = \ Main procedure
$(CIMG_SOLARIS_LDFLAGS) \
$(CIMG_X11_LDFLAGS) \
$(CIMG_XSHM_LDFLAGS) \ --------------------------*/
$int main(CIMG_XRANDR_LDFLAGS) \{
$(CIMG_TIFF_LDFLAGS) \
$(CIMG_MINC2_LDFLAGS) \
$(CIMG_EXR_LDFLAGS) \ // Create a colored 640x480 background image which consists of different color shades.
$ CImg<float> background(CIMG_PNG_LDFLAGS640,480,1,3) \;
$ cimg_forXY(CIMG_JPEG_LDFLAGSbackground,x,y) \background.fillC(x,y,0,
$ x*std::cos(CIMG_ZLIB_LDFLAGS6.0*y/background.height()) + y*std::sin(9.0*x/background.width()) \,
$ x*std::sin(CIMG_OPENCV_LDFLAGS8.0*y/background.height()) - y*std::cos(11.0*x/background.width()) \,
$ x*std::cos(CIMG_MAGICK_LDFLAGS13.0*y/background.height()) - y*std::sin(8.0*x/background.width())) \;
$ background.normalize(CIMG_FFTW3_LDFLAGS0,180)" \;
"STRIP_EXE=true" \
all $(CIMG_EXTRA_FILES) // Init images and create display window.
CImg<unsigned char> img0(background), img;
unsigned char white[] = { 255, 255, 255 }, color[100][3];
CImgDisplay disp(img0,"[#6] - Filled Triangles (Click to shrink)");
# MacOsX targets, with X11 display. // error handling
macosx: cudaError_t err;
@$ // Define random properties (MAKEpos, size, colors, ..) \for all triangles that will be displayed.
"CONF_CFLAGS = \ float posx[100];
$(PROFILE) \ float posy[100];
$(PROFILE2) \ float rayon[100];
$(CIMG_ANSI_CFLAGS) \ float angle[100];
$(CIMG_VT100_CFLAGS) \ float veloc[100];
$(CIMG_X11_CFLAGS)" \ float opacity[100];
"CONF_LDFLAGS = \ // Define the same properties but for the device
$(CIMG_X11_LDFLAGS)" \ float* d_posx;
all float* d_posy;
float* d_rayon;
dmacosx: float* d_angle;
@$(MAKE) \float* d_veloc;
"CONF_CFLAGS = \ float* d_opacity;
$(CIMG_ANSI_CFLAGS) \ unsigned char* d_color;
$(CIMG_DEBUG_CFLAGS) \
$(CIMG_VT100_CFLAGS) \
$(CIMG_X11_CFLAGS)" \ // CURAND state
"CONF_LDFLAGS = \ curandState* devState;
$(CIMG_X11_LDFLAGS)" \
all
// allocate memory on the device for the device arrays
err = cudaMalloc((void**)&d_posx, 100 * sizeof(float));
errCheck(err, "cudaMalloc((void**)&d_posx, 100 * sizeof(float))");
omacosx: err = cudaMalloc((void**)&d_posy, 100 * sizeof(float));
@$errCheck(MAKEerr,"cudaMalloc((void**)&d_posy, 100 * sizeof(float))") \;
"CONF_CFLAGS err = \cudaMalloc((void**)&d_rayon, 100 * sizeof(float));
$ errCheck(CIMG_ANSI_CFLAGSerr,"cudaMalloc((void**)&d_rayon, 100 * sizeof(float))") \;
$ err = cudaMalloc(CIMG_OPT_CFLAGS(void**)&d_angle, 100 * sizeof(float)) \;
$ errCheck(CIMG_VT100_CFLAGSerr,"cudaMalloc((void**)&d_angle, 100 * sizeof(float))") \;
$ err = cudaMalloc(CIMG_X11_CFLAGS(void**)&d_veloc, 100 * sizeof(float))" \;
errCheck(err,"CONF_LDFLAGS = \cudaMalloc((void**)&d_veloc, 100 * sizeof(float))");
$ err = cudaMalloc(CIMG_X11_LDFLAGS(void**)&d_opacity, 100 * sizeof(float))" \;
all errCheck(err,"cudaMalloc((void**)&d_opacity, 100 * sizeof(float))");
err = cudaMalloc((void**)&devState, 100*sizeof(curandState));
mmacosx: errCheck(err,"cudaMalloc((void**)&devState, 100*sizeof(curandState))");
@$(MAKE) \size_t pitch;
"CONF_CFLAGS = \ //allocated the device memory for source array
$ err = cudaMallocPitch(CIMG_ANSI_CFLAGS&d_color, &pitch, 3 * sizeof(unsigned char),100) \;
$ errCheck(CIMG_NODISPLAY_CFLAGSerr,"cudaMallocPitch(&d_color, &pitch, 3 * sizeof(unsigned char),100)") \;
$(CIMG_OPT_CFLAGS)" \ // launch grid of threads
all dim3 dimBlock(100);
dim3 dimGrid(1);
/* Kernel for initializing CURAND */
Mmacosx: initCurand<<<1,100>>>(devState);
@$(MAKE) \
"CONF_CFLAGS = \
$(CIMG_OPT_CFLAGS) \ // synchronize the device and the host
$ cudaDeviceSynchronize(CIMG_VT100_CFLAGS) \;
$(CIMG_X11_CFLAGS) \
$(CIMG_TIFF_CFLAGS) \ /*Kernel for initializing Arrays */
$ initializeArrays<<<1, 100>>>(CIMG_MINC2_CFLAGS) \d_posx, d_posy, d_rayon, d_veloc, d_opacity, d_angle,
$ d_color, img0.height(CIMG_EXR_CFLAGS) \, img0.width(), devState, pitch);
$(CIMG_PNG_CFLAGS) \ // synchronize the device and the host
$ cudaDeviceSynchronize(CIMG_JPEG_CFLAGS) \;
$(CIMG_ZLIB_CFLAGS) \
$(CIMG_OPENCV_CFLAGS) \ // get the populated arrays back to the host for use
$ err = cudaMemcpy(CIMG_MAGICK_CFLAGSposx,d_posx, 100 * sizeof(float), cudaMemcpyDeviceToHost) \;
$ errCheck(CIMG_FFTW3_CFLAGSerr,"cudaMemcpy(posx,d_posx, 100 * sizeof(float), cudaMemcpyDeviceToHost)" \);
"CONF_LDFLAGS err = \cudaMemcpy(posy,d_posy, 100 * sizeof(float), cudaMemcpyDeviceToHost);
$ errCheck(CIMG_X11_LDFLAGSerr,"cudaMemcpy(posy,d_posy, 100 * sizeof(float), cudaMemcpyDeviceToHost)") \;
$ err = cudaMemcpy(CIMG_TIFF_LDFLAGSrayon,d_rayon, 100 * sizeof(float), cudaMemcpyDeviceToHost) \;
$ errCheck(CIMG_MINC2_LDFLAGSerr,"cudaMemcpy(rayon,d_rayon, 100 * sizeof(float), cudaMemcpyDeviceToHost)") \;
$ err = cudaMemcpy(CIMG_EXR_LDFLAGSveloc,d_veloc, 100 * sizeof(float), cudaMemcpyDeviceToHost) \;
$ errCheck(CIMG_PNG_LDFLAGSerr,"cudaMemcpy(veloc,d_veloc, 100 * sizeof(float), cudaMemcpyDeviceToHost)") \;
$ err = cudaMemcpy(CIMG_JPEG_LDFLAGSopacity,d_opacity, 100 * sizeof(float), cudaMemcpyDeviceToHost) \;
$ errCheck(CIMG_ZLIB_LDFLAGSerr,"cudaMemcpy(opacity,d_opacity, 100 * sizeof(float), cudaMemcpyDeviceToHost)") \;
$ err = cudaMemcpy(CIMG_OPENCV_LDFLAGSangle,d_angle, 100 * sizeof(float), cudaMemcpyDeviceToHost) \;
$ errCheck(CIMG_MAGICK_LDFLAGSerr,"cudaMemcpy(angle,d_angle, 100 * sizeof(float), cudaMemcpyDeviceToHost)") \;
$(CIMG_FFTW3_LDFLAGS)" \ // pitch of color array is 3+1 padded
all $ err = cudaMemcpy2D(CIMG_EXTRA_FILEScolor,4,d_color,pitch,3 *sizeof(unsigned char),3, cudaMemcpyDeviceToHost);
errCheck(err,"cudaMemcpy2D(color,pitch,d_color,100*3,3 *sizeof(unsigned char),100* sizeof(unsigned char), cudaMemcpyDeviceToHost)");
// measuring time it takes for triangle animations in 1000 iterations
int i = 0, num = 1;
# Windows targets, with GDI32 display.
windows: // Start animation loop.
@$ while (MAKE!disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC() && i < 1000) \{
"CONF_CFLAGS img = " \img0;
"CONF_LDFLAGS = \
$(CIMG_GDI32_LDFLAGS)" \ i++;
all // Draw each triangle on the background image.
for (int k = 0; k<num; ++k) {
const int
x0 = (int)(posx[k] + rayon[k]*std::cos(angle[k]*cimg::PI/180)),
dwindows y0 = (int)(posy[k] + rayon[k]*std::sin(angle[k]*cimg::PI/180)),
@$ x1 = (MAKEint) \(posx[k] + rayon[k]*std::cos((angle[k] + 120)*cimg::PI/180)),
"CONF_CFLAGS y1 = \(int)(posy[k] + rayon[k]*std::sin((angle[k] + 120)*cimg::PI/180)),
$ x2 = (CIMG_DEBUG_CFLAGSint)" \(posx[k] + rayon[k]*std::cos((angle[k] + 240)*cimg::PI/180)),
"CONF_LDFLAGS y2 = \(int)(posy[k] + rayon[k]*std::sin((angle[k] + 240)*cimg::PI/180));
$ if (CIMG_GDI32_LDFLAGSk%10)" \img.draw_triangle(x0,y0,x1,y1,x2,y2,color[k],opacity[k]);
all else img.draw_triangle(x0,y0,x1,y1,x2,y2,img0,0,0,img0.width()-1,0,0,img.height()-1,opacity[k]);
img.draw_triangle(x0,y0,x1,y1,x2,y2,white,opacity[k],~0U);
// Make the triangles rotate, and check for mouse click event.
owindows: // (to make triangles collapse or join).
@$(MAKE) \ angle[k]+=veloc[k];
"CONF_CFLAGS = \ if (disp.mouse_x()>0 && disp.mouse_y()>0) {
$ float u = disp.mouse_x(CIMG_OPT_CFLAGS)" \- posx[k], v = disp.mouse_y() - posy[k];
"CONF_LDFLAGS if (disp.button()) { u = \-u; v = -v; }
$(CIMG_GDI32_LDFLAGS)" \ posx[k]-=0.03f*u, posy[k]-=0.03f*v;
"STRIP_EXE if (posx[k]<0 || posx[k]>=true" \img.width()) posx[k] = (float)(cimg::rand()*img.width());
all if (posy[k]<0 || posy[k]>=img.height()) posy[k] = (float)(cimg::rand()*img.height());
}
}
mwindows: // Display current animation framerate, and refresh display window.
@$ img.draw_text(MAKE5,5,"%u frames/s",white,0,0.5f,13,(unsigned int)disp.frames_per_second()) \;
"CONF_CFLAGS = \ img0.resize(disp.display(img).resize(false).wait(20));
$ if (CIMG_NODISPLAY_CFLAGS++num>100) \num = 100;
$(CIMG_OPT_CFLAGS)" \
"STRIP_EXE=true" \ // Allow the user to toggle fullscreen mode, by pressing CTRL+F.
all if (disp.is_keyCTRLLEFT() && disp.is_keyF()) disp.resize(640,480,false).toggle_fullscreen(false);
}
Mwindows: // free allocated device memory
@$cudaFree(MAKEd_posy) \;
"CONF_CFLAGS = \ cudaFree(d_posx);
$ cudaFree(CIMG_OPT_CFLAGSd_rayon) \;
$ cudaFree(CIMG_TIFF_CFLAGSd_veloc) \;
$ cudaFree(CIMG_PNG_CFLAGSd_opacity) \;
$ cudaFree(CIMG_JPEG_CFLAGSd_color) \;
$ cudaFree(CIMG_ZLIB_CFLAGSd_angle) \;
$ cudaFree(CIMG_OPENCV_CFLAGSdevState) \;
$(CIMG_FFTW3_CFLAGS)" \ return 0;
"CONF_LDFLAGS = \}
$(CIMG_GDI32_LDFLAGS) \
$(CIMG_TIFF_LDFLAGS) \</source>
$(CIMG_PNG_LDFLAGS) \=== Profiling Results ===The entirety of the GPU code executes in 0.15 - 0.16 seconds. I profiled the application using nvvp and NSight on Visual Studio 2010.=== Issues Encountered ===
$(CIMG_JPEG_LDFLAGS) \Makefile does not work on the Windows platform. Tried adding the -lcurand linker flag but to no avail.
$(CIMG_ZLIB_LDFLAGS) \The code does compile cleanly without error on Visual Studio 2010 IDE.
$(CIMG_OPENCV_LDFLAGS) \Coding issues:
$(CIMG_FFTW3_LDFLAGS)" \Stackoverflow Questions I made:
"STRIP_EXE=true" \[http://stackoverflow.com/questions/15245723/using-arrayij-in-a-cuda-kernel-memcpy-call Using 2D Arrays]
all $(CIMG_EXTRA_FILES)[http://stackoverflow.com/questions/15238009/concurrently-initializing-many-arrays-with-random-numbers-using-curand-and-cuda Memory Issues]
</pre></big>
=== Build Instructions ===
make <platform> eg: macosx, linux etc.
== Assignment 2 ==With the help of the open-source community I was able to solve my problems and gain a better understanding of CUDA and CURAND.
== Assignment 3 ==
1
edit

Navigation menu