Skip to content

Submission: Bradley Crusco #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0a35e8c
Rough implementation of the accelerate function, including gravitatio…
bcrusco Sep 1, 2015
e7ad832
Rewrote accelerate and gravitationalAccelerationHelper to use the cor…
bcrusco Sep 1, 2015
fad12a4
Add a safety check to avoid dividing by very small numbers.
bcrusco Sep 1, 2015
169b865
Minor code cleanup, added screenshot.
bcrusco Sep 1, 2015
3fce39f
Basic setup for part 2 of the project.
bcrusco Sep 2, 2015
a233bce
Basic idea of main and the initialization function.
bcrusco Sep 2, 2015
d562c39
Add skeleton functions for the matrix operations.
bcrusco Sep 2, 2015
4a8dbec
Add the basic functionality for copying memory between the device and…
bcrusco Sep 3, 2015
870f848
Basic functionality working for matrix addition and subtraction.
bcrusco Sep 3, 2015
659ef04
Basic implementation of matrix mult
bcrusco Sep 3, 2015
6896bad
Updated the direction of gravity and took a new screenshot.
bcrusco Sep 3, 2015
d9edb59
Added some more robust testing of the addition subtraction and multip…
bcrusco Sep 3, 2015
be16cd3
Add error check for host allocations, clean up code formatting and or…
bcrusco Sep 3, 2015
332df4c
Removed unused references, fixed erroneous error messages.
bcrusco Sep 3, 2015
20a8e1f
Fixed typo
bcrusco Sep 3, 2015
67a7c8b
Remove instructions, add basic performance analysis
bcrusco Sep 4, 2015
47bb6e0
Update README.md
bcrusco Sep 4, 2015
41d805b
Update README.md
bcrusco Sep 4, 2015
44b4be3
Add analysis graphs
bcrusco Sep 4, 2015
6d94aeb
Merge branch 'master' of https://github.com/bcrusco/Project1-CUDA-Int…
bcrusco Sep 4, 2015
a58100d
Update README.md
bcrusco Sep 4, 2015
fe85688
Update README.md
bcrusco Sep 4, 2015
f7c9d96
Update README.md
bcrusco Sep 4, 2015
7ad77d4
Update README.md
bcrusco Sep 4, 2015
ff8bfca
Update README.md
bcrusco Sep 4, 2015
f23e0f2
Update README.md
bcrusco Sep 4, 2015
1fd6ff4
Update README.md
bcrusco Sep 4, 2015
7cb4d80
Screen shot of the matrix math test running.
bcrusco Sep 4, 2015
82e23a1
Update README.md
bcrusco Sep 4, 2015
2b636e8
Update README.md
bcrusco Sep 4, 2015
d79b972
Update README.md
bcrusco Sep 4, 2015
cdcbad7
Update README.md
bcrusco Sep 4, 2015
1a02fe1
Update README.md
bcrusco Sep 4, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions Project1-Part1/src/kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ void Nbody::copyPlanetsToVBO(float *vbodptr) {
* stepSimulation *
******************/

/**
* Compute the acceleration on a body at `my_pos` due a single other body
*/
__device__ glm::vec3 gravitationalAccelerationHelper(glm::vec3 this_planet, glm::vec3 other_body, float other_body_mass) {
// Check our distance squared calculation to protect against dividing by too small a number
float distance_squared = glm::pow(glm::distance(this_planet, other_body), 2);
if (glm::abs(distance_squared) < EPSILON) {
distance_squared = EPSILON;
}
glm::vec3 unit_direction = glm::normalize(other_body - this_planet);
float g = (G * other_body_mass) / distance_squared;

return g * unit_direction;
}

/**
* Compute the acceleration on a body at `my_pos` due to the `N` bodies in the array `other_planets`.
*/
Expand All @@ -189,8 +204,22 @@ __device__ glm::vec3 accelerate(int N, int iSelf, glm::vec3 this_planet, const
// * G is the universal gravitational constant (already defined for you)
// * M is the mass of the other object
// * r is the distance between this object and the other object

return glm::vec3(0.0f);
// * Also need to multiply the result by the unit vector of r from the other object to this planet
// * And should this be negative? Because this is an attractive force

glm::vec3 totalAcceleration = glm::vec3(0.0f);

// Calculate acceleration relative to the start
totalAcceleration += gravitationalAccelerationHelper(this_planet, glm::vec3(0.0f), starMass);

// Calculate acceleration relative to the other planets
for (int i = 0; i < N; i++) {
if (i != iSelf) {
totalAcceleration += gravitationalAccelerationHelper(this_planet, other_planets[i], planetMass);
}
}

return totalAcceleration;
}

/**
Expand All @@ -201,6 +230,9 @@ __global__ void kernUpdateAcc(int N, float dt, const glm::vec3 *pos, glm::vec3 *
// TODO: implement updateAccArray.
// This function body runs once on each CUDA thread.
// To avoid race conditions, each instance should only write ONE value to `acc`!
int index = (blockIdx.x * blockDim.x) + threadIdx.x;

acc[index] = accelerate(N, index, pos[index], pos);
}

/**
Expand All @@ -209,6 +241,10 @@ __global__ void kernUpdateAcc(int N, float dt, const glm::vec3 *pos, glm::vec3 *
*/
__global__ void kernUpdateVelPos(int N, float dt, glm::vec3 *pos, glm::vec3 *vel, const glm::vec3 *acc) {
// TODO: implement updateVelocityPosition
int index = (blockIdx.x * blockDim.x) + threadIdx.x;

vel[index] = vel[index] + acc[index] * dt;
pos[index] = pos[index] + vel[index] * dt;
}

/**
Expand All @@ -217,4 +253,11 @@ __global__ void kernUpdateVelPos(int N, float dt, glm::vec3 *pos, glm::vec3 *vel
void Nbody::stepSimulation(float dt) {
// TODO: Using the CUDA kernels you wrote above, write a function that
// calls the kernels to perform a full simulation step.
dim3 fullBlocksPerGrid((numObjects + blockSize - 1) / blockSize);

// Update acceleration
kernUpdateAcc<<<fullBlocksPerGrid, blockSize>>>(numObjects, dt, dev_pos, dev_acc);

// Update velocity and position
kernUpdateVelPos<<<fullBlocksPerGrid, blockSize>>>(numObjects, dt, dev_pos, dev_vel, dev_acc);
}
84 changes: 84 additions & 0 deletions Project1-Part2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
cmake_minimum_required(VERSION 3.0)

project(cis565_matrix_math)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

# Set up include and lib paths
set(EXTERNAL "external")
include_directories("${EXTERNAL}/include")
include_directories("${EXTERNAL}/src")
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(EXTERNAL_LIB_PATH "${EXTERNAL}/lib/osx")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(EXTERNAL_LIB_PATH "${EXTERNAL}/lib/linux" "/usr/lib64")
elseif(WIN32)
set(EXTERNAL_LIB_PATH "${EXTERNAL}/lib/win")
endif()
link_directories(${EXTERNAL_LIB_PATH})
list(APPEND CMAKE_LIBRARY_PATH "${EXTERNAL_LIB_PATH}")

# Find up and set up core dependency libs

set(GLFW_INCLUDE_DIR "${EXTERNAL}/include")
set(GLFW_LIBRARY_DIR "${CMAKE_LIBRARY_PATH}")
find_library(GLFW_LIBRARY "glfw3" HINTS "${GLFW_LIBRARY_DIR}")

set(GLEW_INCLUDE_DIR "${EXTERNAL}/include")
set(GLEW_LIBRARY_DIR "${CMAKE_LIBRARY_PATH}")
add_definitions(-DGLEW_STATIC)
find_package(GLEW)

find_package(OpenGL)

set(CORELIBS
"${GLFW_LIBRARY}"
"${OPENGL_LIBRARY}"
"${GLEW_LIBRARY}"
)

# Enable C++11 for host code
set(CMAKE_CXX_STANDARD 11)

# OSX-specific hacks/fixes
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
list(APPEND CORELIBS "-framework IOKit")
list(APPEND CORELIBS "-framework Cocoa")
list(APPEND CORELIBS "-framework CoreVideo")
endif()

# Linux-specific hacks/fixes
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
list(APPEND CMAKE_EXE_LINKER_FLAGS "-lX11 -lXxf86vm -lXrandr -lpthread -lXi")
endif()

# Crucial magic for CUDA linking
find_package(Threads REQUIRED)
find_package(CUDA REQUIRED)

set(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE ON)
set(CUDA_SEPARABLE_COMPILATION ON)

if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
endif()

add_subdirectory(src)

cuda_add_executable(${CMAKE_PROJECT_NAME}
"src/main.hpp"
"src/main.cpp"
)

target_link_libraries(${CMAKE_PROJECT_NAME}
src
${CORELIBS}
)

add_custom_command(
TARGET ${CMAKE_PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/shaders
${CMAKE_BINARY_DIR}/shaders
)
31 changes: 31 additions & 0 deletions Project1-Part2/GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CMAKE_ALT1 := /usr/local/bin/cmake
CMAKE_ALT2 := /Applications/CMake.app/Contents/bin/cmake
CMAKE := $(shell \
which cmake 2>/dev/null || \
([ -e ${CMAKE_ALT1} ] && echo "${CMAKE_ALT1}") || \
([ -e ${CMAKE_ALT2} ] && echo "${CMAKE_ALT2}") \
)

all: RelWithDebugInfo


Debug: build
(cd build && ${CMAKE} -DCMAKE_BUILD_TYPE=$@ .. && make)

MinSizeRel: build
(cd build && ${CMAKE} -DCMAKE_BUILD_TYPE=$@ .. && make)

Release: build
(cd build && ${CMAKE} -DCMAKE_BUILD_TYPE=$@ .. && make)

RelWithDebugInfo: build
(cd build && ${CMAKE} -DCMAKE_BUILD_TYPE=$@ .. && make)


build:
(mkdir -p build && cd build)

clean:
((cd build && make clean) 2>&- || true)

.PHONY: all Debug MinSizeRel Release RelWithDebugInfo clean
161 changes: 161 additions & 0 deletions Project1-Part2/cmake/CMakeParseArguments.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#.rst:
# CMakeParseArguments
# -------------------
#
#
#
# CMAKE_PARSE_ARGUMENTS(<prefix> <options> <one_value_keywords>
# <multi_value_keywords> args...)
#
# CMAKE_PARSE_ARGUMENTS() is intended to be used in macros or functions
# for parsing the arguments given to that macro or function. It
# processes the arguments and defines a set of variables which hold the
# values of the respective options.
#
# The <options> argument contains all options for the respective macro,
# i.e. keywords which can be used when calling the macro without any
# value following, like e.g. the OPTIONAL keyword of the install()
# command.
#
# The <one_value_keywords> argument contains all keywords for this macro
# which are followed by one value, like e.g. DESTINATION keyword of the
# install() command.
#
# The <multi_value_keywords> argument contains all keywords for this
# macro which can be followed by more than one value, like e.g. the
# TARGETS or FILES keywords of the install() command.
#
# When done, CMAKE_PARSE_ARGUMENTS() will have defined for each of the
# keywords listed in <options>, <one_value_keywords> and
# <multi_value_keywords> a variable composed of the given <prefix>
# followed by "_" and the name of the respective keyword. These
# variables will then hold the respective value from the argument list.
# For the <options> keywords this will be TRUE or FALSE.
#
# All remaining arguments are collected in a variable
# <prefix>_UNPARSED_ARGUMENTS, this can be checked afterwards to see
# whether your macro was called with unrecognized parameters.
#
# As an example here a my_install() macro, which takes similar arguments
# as the real install() command:
#
# ::
#
# function(MY_INSTALL)
# set(options OPTIONAL FAST)
# set(oneValueArgs DESTINATION RENAME)
# set(multiValueArgs TARGETS CONFIGURATIONS)
# cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}"
# "${multiValueArgs}" ${ARGN} )
# ...
#
#
#
# Assume my_install() has been called like this:
#
# ::
#
# my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub)
#
#
#
# After the cmake_parse_arguments() call the macro will have set the
# following variables:
#
# ::
#
# MY_INSTALL_OPTIONAL = TRUE
# MY_INSTALL_FAST = FALSE (this option was not used when calling my_install()
# MY_INSTALL_DESTINATION = "bin"
# MY_INSTALL_RENAME = "" (was not used)
# MY_INSTALL_TARGETS = "foo;bar"
# MY_INSTALL_CONFIGURATIONS = "" (was not used)
# MY_INSTALL_UNPARSED_ARGUMENTS = "blub" (no value expected after "OPTIONAL"
#
#
#
# You can then continue and process these variables.
#
# Keywords terminate lists of values, e.g. if directly after a
# one_value_keyword another recognized keyword follows, this is
# interpreted as the beginning of the new option. E.g.
# my_install(TARGETS foo DESTINATION OPTIONAL) would result in
# MY_INSTALL_DESTINATION set to "OPTIONAL", but MY_INSTALL_DESTINATION
# would be empty and MY_INSTALL_OPTIONAL would be set to TRUE therefor.

#=============================================================================
# Copyright 2010 Alexander Neundorf <[email protected]>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)


if(__CMAKE_PARSE_ARGUMENTS_INCLUDED)
return()
endif()
set(__CMAKE_PARSE_ARGUMENTS_INCLUDED TRUE)


function(CMAKE_PARSE_ARGUMENTS prefix _optionNames _singleArgNames _multiArgNames)
# first set all result variables to empty/FALSE
foreach(arg_name ${_singleArgNames} ${_multiArgNames})
set(${prefix}_${arg_name})
endforeach()

foreach(option ${_optionNames})
set(${prefix}_${option} FALSE)
endforeach()

set(${prefix}_UNPARSED_ARGUMENTS)

set(insideValues FALSE)
set(currentArgName)

# now iterate over all arguments and fill the result variables
foreach(currentArg ${ARGN})
list(FIND _optionNames "${currentArg}" optionIndex) # ... then this marks the end of the arguments belonging to this keyword
list(FIND _singleArgNames "${currentArg}" singleArgIndex) # ... then this marks the end of the arguments belonging to this keyword
list(FIND _multiArgNames "${currentArg}" multiArgIndex) # ... then this marks the end of the arguments belonging to this keyword

if(${optionIndex} EQUAL -1 AND ${singleArgIndex} EQUAL -1 AND ${multiArgIndex} EQUAL -1)
if(insideValues)
if("${insideValues}" STREQUAL "SINGLE")
set(${prefix}_${currentArgName} ${currentArg})
set(insideValues FALSE)
elseif("${insideValues}" STREQUAL "MULTI")
list(APPEND ${prefix}_${currentArgName} ${currentArg})
endif()
else()
list(APPEND ${prefix}_UNPARSED_ARGUMENTS ${currentArg})
endif()
else()
if(NOT ${optionIndex} EQUAL -1)
set(${prefix}_${currentArg} TRUE)
set(insideValues FALSE)
elseif(NOT ${singleArgIndex} EQUAL -1)
set(currentArgName ${currentArg})
set(${prefix}_${currentArgName})
set(insideValues "SINGLE")
elseif(NOT ${multiArgIndex} EQUAL -1)
set(currentArgName ${currentArg})
set(${prefix}_${currentArgName})
set(insideValues "MULTI")
endif()
endif()

endforeach()

# propagate the result variables to the caller:
foreach(arg_name ${_singleArgNames} ${_multiArgNames} ${_optionNames})
set(${prefix}_${arg_name} ${${prefix}_${arg_name}} PARENT_SCOPE)
endforeach()
set(${prefix}_UNPARSED_ARGUMENTS ${${prefix}_UNPARSED_ARGUMENTS} PARENT_SCOPE)

endfunction()
Loading