diff --git a/.editorconfig b/.editorconfig index 71375f506accd4b98de17df3277917ceb7203dfe..cf62274f9b7d9a7cb18975e170305e51d0acf712 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,7 +3,7 @@ # See https://editorconfig.org/ for documentation. # # https://git.brandon-rodriguez.com/other/editorconfig - # Version 1.0 + # Version 1.1 ## @@ -26,7 +26,7 @@ trim_trailing_whitespace = true # Tab styles that should be four spaces per tab. # Most languages will fall under this. ## -[*.{c,cc,cpp,h,json,md,o,sh,yaml}] +[*.{c,cc,cpp,cu,h,json,md,o,sh,yaml}] indent_size = 4 diff --git a/part_1/main.cu b/part_1/main.cu new file mode 100644 index 0000000000000000000000000000000000000000..c6c30b3ff5c948cffa84da4cfa1ffe1be563d5bb --- /dev/null +++ b/part_1/main.cu @@ -0,0 +1,77 @@ +/** + * + */ + + +// System Import Headers. +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +// Method Declaration. +void compute(int a, int b); +__global__ void cuda_add(int a, int b, int* c); + + +// Global Variables. + + +/* + * Program's main. Initializes and runs program. + */ +int main(int argc, char* argv[]) { + printf("Starting program.\n"); + printf("\n"); + + // Check provided number of args. + if (argc < 3) { + // Less than two args provided. + printf("Provided too few program args. Expected two integers to add."); + } else if (argc > 3) { + // More than two args provided. + printf("Provided too many program args. Expected two integers to add."); + } else { + // Two args provided. Compute with GPU. + int a = atoi(argv[1]); + int b = atoi(argv[2]); + printf("Adding %i and %i\n", a, b); + compute(a, b); + } + + printf("\n"); + printf("Terminating program.\n"); + exit(0); +} + + +/* + * Function to have CPU and GPU collaborate for basic addition computation. + */ +void compute(int a, int b) { + int sum; + int* dev_sum; + + // Allocate memory on the GPU. + cudaMalloc((int**) &dev_sum, sizeof(int)); + + // Call CUDA function. + cuda_add<<<1,1>>>(a, b, dev_sum); + + // Copy GPU result back to CPU. + cudaMemcpy(&sum, dev_sum, sizeof(int), cudaMemcpyDeviceToHost); + + // Display result. + printf("Sum is %i\n", sum); + + cudaFree(dev_sum); +} + + +/** + * Entrypoint to GPU kernel execution. + */ +__global__ void cuda_add(int a, int b, int* c) { + *c = a + b; +} diff --git a/part_1/makefile b/part_1/makefile new file mode 100644 index 0000000000000000000000000000000000000000..110eeb1edf27c6de43b919e2d49b1cc1115ff9d3 --- /dev/null +++ b/part_1/makefile @@ -0,0 +1,35 @@ + +# Tell makefile to use commands defined here, if file with same name exists. +.PHONY: all run valgrind +# Set default if none is specified. +default: all + + +# Variables. +CC = nvcc +CFLAGS = +TARGET = main.out +LIBRARIES = +DEPENDENCIES = ./main.cu +# ARGS = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}` +ARGS = $(filter-out $@,$(MAKECMDGOALS)) + + +# Compile target if any depenencies update. +$(TARGET): $(DEPENDENCIES) + @$(CC) $(CFLAGS) $(DEPENDENCIES) -o $(TARGET) $(LIBRARIES) + + +# Compile target if dependencies update. +all: $(TARGET) + + +# Compile target if dependencies update. Then run. +run: $(TARGET) + @./$(TARGET) $(ARGS) + + +# Compile target if dependencies update. Then run. +valgrind: $(TARGET) + @rm ./logs/vg/* + valgrind --leak-check=full --suppressions=valgrind_supression.txt --log-file=./logs/vg/vg.%p ./$(TARGET) $(ARGS)