Skip to content
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

Submission: Ziwei Zong #10

Open
wants to merge 6 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
62 changes: 59 additions & 3 deletions Project1-Part1/src/kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cuda.h>
#include <cmath>
#include <glm/glm.hpp>
#include <cuda_runtime.h>
#include "utilityCore.hpp"
#include "kernel.h"

Expand All @@ -28,7 +29,7 @@ void checkCUDAError(const char *msg, int line = -1) {
*****************/

/*! Block size used for CUDA kernel launch. */
#define blockSize 128
#define blockSize 1024

/*! Mass of one "planet." */
#define planetMass 3e8f
Expand All @@ -39,6 +40,7 @@ void checkCUDAError(const char *msg, int line = -1) {
/*! Size of the starting area in simulation space. */
const float scene_scale = 1e2;

const int ApprStep = 1;

/***********************************************
* Kernel state (pointers are device pointers) *
Expand Down Expand Up @@ -172,6 +174,15 @@ void Nbody::copyPlanetsToVBO(float *vbodptr) {
/**
* Compute the acceleration on a body at `my_pos` due to the `N` bodies in the array `other_planets`.
*/

__device__ glm::vec3 accBySingleOtherPlanet(glm::vec3 this_planet,glm::vec3 other_planet,float mass)
{
float dist = glm::length(other_planet - this_planet);
dist *= dist;
float gMag = G*mass / (dist + ZERO_ABSORPTION_EPSILON);//!!! divide by zero
glm::vec3 g = glm::normalize(other_planet - this_planet)*gMag;
return g;
}
__device__ glm::vec3 accelerate(int N, int iSelf, glm::vec3 this_planet, const glm::vec3 *other_planets) {
// TODO: Compute the acceleration on `my_pos` due to:
// * The star at the origin (with mass `starMass`)
Expand All @@ -189,8 +200,21 @@ __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 g(0.0f);

//g by Origin Star
g += accBySingleOtherPlanet(this_planet,glm::vec3(0.0f),starMass);
//g by others

for (int i = 0; i < N; i += ApprStep)
{
if (i != iSelf)
{
g += accBySingleOtherPlanet(this_planet, other_planets[i],planetMass);
}
}
//return glm::vec3(0.0f);
return g;
}

/**
Expand All @@ -199,6 +223,11 @@ __device__ glm::vec3 accelerate(int N, int iSelf, glm::vec3 this_planet, const
*/
__global__ void kernUpdateAcc(int N, float dt, const glm::vec3 *pos, glm::vec3 *acc) {
// TODO: implement updateAccArray.
int idx = (blockIdx.x * blockDim.x) + threadIdx.x;
if (idx < N)
{
acc[idx] = accelerate(N, idx, pos[idx], pos);
}
// This function body runs once on each CUDA thread.
// To avoid race conditions, each instance should only write ONE value to `acc`!
}
Expand All @@ -209,6 +238,11 @@ __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 idx = (blockIdx.x * blockDim.x) + threadIdx.x;
if (idx < N){
vel[idx] += acc[idx] * dt;
pos[idx] += vel[idx] * dt;
}
}

/**
Expand All @@ -217,6 +251,28 @@ __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);

cudaEvent_t start, stop;
float elapsedTime;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start,0);

kernUpdateAcc <<< fullBlocksPerGrid, threadsPerBlock >>>(numObjects, dt, dev_pos, dev_acc);

cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
printf( "\n1 -- kernUpdateAcc Time = %.2fms",elapsedTime );


cudaEventRecord(start, 0);
kernUpdateVelPos <<< fullBlocksPerGrid, threadsPerBlock >> > (numObjects, dt, dev_pos, dev_vel, dev_acc);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
printf("\n2 -- kernUpdateVelPos Time = %.4fms", elapsedTime);
}

void Nbody::endSimulation()
Expand Down
4 changes: 2 additions & 2 deletions Project1-Part1/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#define VISUALIZE 1

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

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

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

glfwSwapBuffers(window);
#endif
}
glfwDestroyWindow(window);
glfwTerminate();
Expand Down
2 changes: 1 addition & 1 deletion Project1-Part1/src/utilityCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define E 2.7182818284590452353602874713526624977572
#define G 6.67384e-11
#define EPSILON .000000001
#define ZERO_ABSORPTION_EPSILON 0.00001
#define ZERO_ABSORPTION_EPSILON 0.01

namespace utilityCore {
extern float clamp(float f, float min, float max);
Expand Down
83 changes: 83 additions & 0 deletions Project1-Part2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
cmake_minimum_required(VERSION 3.0)

project(cis565_nbody)

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.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