Skip to content

Submission: SANCHIT GARG #19

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 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions Project1-Part1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ set(CORELIBS
# Enable C++11 for host code
set(CMAKE_CXX_STANDARD 11)

list(APPEND CUDA_NVCC_FLAGS -G -g)

# OSX-specific hacks/fixes
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
list(APPEND CORELIBS "-framework IOKit")
Expand Down
60 changes: 58 additions & 2 deletions Project1-Part1/src/kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ void Nbody::copyPlanetsToVBO(float *vbodptr) {
* stepSimulation *
******************/

__device__ glm::vec3 computeNeighbourForce(glm::vec3 this_planet_position, glm::vec3 that_planet_position, float mass)
{
//TODO: check distance is not very small
float dis = (glm::distance(this_planet_position, that_planet_position));

if(dis < 1.f)
return glm::vec3(0.0f);

return ( ( (that_planet_position - this_planet_position) / dis) * (float) ( (G * mass) / (dis * dis)) ) ;
}

/**
* Compute the acceleration on a body at `my_pos` due to the `N` bodies in the array `other_planets`.
*/
Expand All @@ -189,8 +200,18 @@ __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);

glm::vec3 neighbourForce(0.0f);

neighbourForce = computeNeighbourForce(this_planet, glm::vec3(0.0f), starMass);

for(int i=0; i<N; i++)
{
if(i != iSelf)
neighbourForce += computeNeighbourForce(this_planet, other_planets[i], planetMass);
}

return neighbourForce;
}

/**
Expand All @@ -201,6 +222,13 @@ __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 = threadIdx.x + (blockIdx.x * blockDim.x);

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

/**
Expand All @@ -209,6 +237,14 @@ __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 = threadIdx.x + (blockIdx.x * blockDim.x);

if(index < N)
{
vel[index] += acc[index] * dt;
pos[index] += vel[index] * dt;
}
}

/**
Expand All @@ -217,4 +253,24 @@ __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.

// //For actual simulation
// dim3 fullBlocksPerGrid((numObjects + blockSize - 1) / blockSize);
//
// kernUpdateAcc<<<fullBlocksPerGrid, blockSize>>>(numObjects, dt, dev_pos, dev_acc);
// kernUpdateVelPos<<<fullBlocksPerGrid, blockSize>>>(numObjects, dt, dev_pos, dev_vel, dev_acc);

//For performance analysis
int analysisGridSize = 32;
int analysisBlockSize = 128;

kernUpdateAcc<<<analysisGridSize, analysisBlockSize>>>(numObjects, dt, dev_pos, dev_acc);
kernUpdateVelPos<<<analysisGridSize, analysisBlockSize>>>(numObjects, dt, dev_pos, dev_vel, dev_acc);
}

//void Nbody::endSimulation()
//{
// cudaFree(dev_acc);
// cudaFree(dev_vel);
// cudaFree(dev_pos);
//}
8 changes: 4 additions & 4 deletions Project1-Part1/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// Configuration
// ================

#define VISUALIZE 1
#define VISUALIZE 0

const int N_FOR_VIS = 5000;
const int N_FOR_VIS = 4096;
const float DT = 0.2f;

/**
Expand Down Expand Up @@ -237,9 +237,9 @@ void mainLoop() {

glUseProgram(0);
glBindVertexArray(0);
#endif

glfwSwapBuffers(window);

#endif
}
glfwDestroyWindow(window);
glfwTerminate();
Expand Down
86 changes: 86 additions & 0 deletions Project1-Part2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
cmake_minimum_required(VERSION 3.0)

project(cis565_matMath)

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)

list(APPEND CUDA_NVCC_FLAGS -G -g)

# 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
Loading