Skip to content
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
Binary file modified Part1/PROJ_WIN/CIS565_PROJ_1.suo
Binary file not shown.
4 changes: 2 additions & 2 deletions Part1/PROJ_WIN/CIS565_PROJ_1/CIS565_PROJ_1.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 5.5.props" />
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 6.5.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
Expand Down Expand Up @@ -114,6 +114,6 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 5.5.targets" />
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 6.5.targets" />
</ImportGroup>
</Project>
5 changes: 5 additions & 0 deletions Part1/PROJ_WIN/CIS565_PROJ_1/CIS565_PROJ_1.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerEnvironment>
</LocalDebuggerEnvironment>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
Binary file modified Part1/PROJ_WIN/CIS565_PROJ_1/vc100.pdb
Binary file not shown.
740 changes: 370 additions & 370 deletions Part1/PROJ_WIN/src/kernel.cu.deps

Large diffs are not rendered by default.

39 changes: 35 additions & 4 deletions Part1/src/kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,47 @@ __global__ void generateCircularVelArray(int time, int N, glm::vec3 * arr, glm::
// REMEMBER : F = (G * m_a * m_b) / (r_ab ^ 2)
__device__ glm::vec3 accelerate(int N, glm::vec4 my_pos, glm::vec4 * their_pos)
{
return glm::vec3(0.0f);
int index = (blockIdx.x * blockDim.x) + threadIdx.x;
glm::vec3 r;
//float mass = my_pos.z;
glm::vec3 acc = glm::vec3(0.0f,0.0f,0.0f);
glm::vec3 pos(my_pos.x, my_pos.y, my_pos.z);
float l = glm::length(pos);
for(int i = 0;i < N; i++)
{
if(i != index)
{
r = glm::vec3(their_pos[i].x-my_pos.x, their_pos[i].y-my_pos.y, their_pos[i].z-my_pos.z);
float l = glm::length(r);
glm::vec3 mag = r / l;
acc += (float)( (float)their_pos[i].w / powf(l * l + ZERO_ABSORPTION_EPSILON * ZERO_ABSORPTION_EPSILON,3/2)) * mag;
//acc += r * (float) (their_pos[i].w / (l* l * l + EPSILON)) ;
}
}
acc += (float)( (float)starMass / powf(l * l + ZERO_ABSORPTION_EPSILON * ZERO_ABSORPTION_EPSILON,3/2)) * (-pos)/glm::length(-pos);
//acc += (-pos) * (float)(starMass / ( s * s * s + EPSILON )) ;
return (float)G * acc;
}

// TODO : update the acceleration of each body
__global__ void updateF(int N, float dt, glm::vec4 * pos, glm::vec3 * vel, glm::vec3 * acc)
{
// FILL IN HERE
int index = (blockIdx.x * blockDim.x) + threadIdx.x;
if(index < N)
{
acc[index] = accelerate(N, pos[index], pos) ;
}
}

// TODO : update velocity and position using a simple Euler integration scheme
__global__ void updateS(int N, float dt, glm::vec4 * pos, glm::vec3 * vel, glm::vec3 * acc)
{
// FILL IN HERE
int index = (blockIdx.x * blockDim.x) + threadIdx.x;
if(index < N)
{
pos[index] += glm::vec4(vel[index].x, vel[index].y, vel[index].z, 0.0f) * dt;
vel[index] += acc[index] * dt;
}
}

// Update the vertex buffer object
Expand Down Expand Up @@ -179,7 +207,10 @@ void initCuda(int N)
// TODO : Using the functions you wrote above, write a function that calls the CUDA kernels to update a single sim step
void cudaNBodyUpdateWrapper(float dt)
{
// FILL IN HERE
dim3 fullBlocksPerGrid((int)ceil(float(numObjects)/float(blockSize)));
updateF<<<fullBlocksPerGrid, blockSize>>>( numObjects, dt, dev_pos, dev_vel, dev_acc);
updateS<<<fullBlocksPerGrid, blockSize>>>( numObjects, dt, dev_pos, dev_vel, dev_acc);
cudaThreadSynchronize();
}

void cudaUpdateVBO(float * vbodptr, int width, int height)
Expand Down
4 changes: 2 additions & 2 deletions Part1/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "main.h"

#define N_FOR_VIS 5000
#define DT 0.2
#define N_FOR_VIS 2000
#define DT 0.1
#define VISUALIZE 1
//-------------------------------
//-------------MAIN--------------
Expand Down
20 changes: 20 additions & 0 deletions Part2/matrix/matrix.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "matrix", "matrix\matrix.vcxproj", "{6D40842A-A7DB-4254-9652-8C9A21F55B4F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6D40842A-A7DB-4254-9652-8C9A21F55B4F}.Debug|Win32.ActiveCfg = Debug|Win32
{6D40842A-A7DB-4254-9652-8C9A21F55B4F}.Debug|Win32.Build.0 = Debug|Win32
{6D40842A-A7DB-4254-9652-8C9A21F55B4F}.Release|Win32.ActiveCfg = Release|Win32
{6D40842A-A7DB-4254-9652-8C9A21F55B4F}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
76 changes: 76 additions & 0 deletions Part2/matrix/matrix/matrix.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{6D40842A-A7DB-4254-9652-8C9A21F55B4F}</ProjectGuid>
<RootNamespace>matrix</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 6.5.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;cudart.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<CudaCompile>
<Include>$(CudaToolkitIncludeDir)</Include>
</CudaCompile>
</ItemDefinitionGroup>
<ItemGroup>
<CudaCompile Include="matrix_math.cu">
<FileType>Document</FileType>
</CudaCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA 6.5.targets" />
</ImportGroup>
</Project>
22 changes: 22 additions & 0 deletions Part2/matrix/matrix/matrix.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<CudaCompile Include="matrix_math.cu">
<Filter>Source Files</Filter>
</CudaCompile>
</ItemGroup>
</Project>
134 changes: 134 additions & 0 deletions Part2/matrix/matrix/matrix_math.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <cuda.h>
#include <cuda_runtime.h>
#include <iostream>
#include <math.h>

using namespace std;
const int N = 5;

//data on device
float * dev_matA;
float * dev_matB;
float * dev_res;

//data on host
float * matA = (float *)malloc(N*N*sizeof(float));
float * matB = (float *)malloc(N*N*sizeof(float));
float * res = (float *)malloc(N*N*sizeof(float));

void initMat(float * mat)
{
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
mat[i + j*N] = i + j * N;
}

void printRes()
{
for(int j =0; j <N; j++)
{
cout << endl;
for(int i=0;i<N;i++)
cout << res[j*N + i] << " ";
}
}

__global__ void mat_add(float * A, float * B, float * res)
{
int x = threadIdx.x + blockIdx.x *blockDim.x;
int y = threadIdx.y + blockIdx.y *blockDim.y;
res[x + y*N] = A[x + y*N] + B[x + y*N];
}

__global__ void mat_sub(float * A, float * B, float * res)
{
int x = threadIdx.x + blockIdx.x *blockDim.x;
int y = threadIdx.y + blockIdx.y *blockDim.y;
res[x + y*N] = A[x + y*N] - B[x + y*N];
}

__global__ void mat_mult(float * A, float * B, float * res)
{
int x = threadIdx.x + blockIdx.x *blockDim.x;
int y = threadIdx.y + blockIdx.y *blockDim.y;

float result = 0;
for(int i = 0; i < N; i++)
{
result += A[y * N + i] * B[x + i * N];
}
res[x + y*N] = result;
}

void mat_add_serial(float * A, float * B, float * res)
{
for(int i = 0; i < N * N; i++)
{
res[i] = A[i] + B[i];
}
}
void mat_sub_serial(float * A, float * B, float * res)
{
for(int i = 0; i < N * N; i++)
{
res[i] = A[i] - B[i];
}
}
void mat_mult_serial(float * A, float * B, float * res)
{
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
{
float result = 0;
for(int k = 0; k < N; k++)
{
result += A[i*N + k] * B[k*N + j];
}
res[i*N + j] = result;
}
}

int main(int argc, char** argv)
{
dim3 threadsPerBlock(16,16);
dim3 numBlocks((int)ceil((float)N / threadsPerBlock.x), (int)ceil((float)N/threadsPerBlock.y));

cudaMalloc((void**)&dev_matA, N*N*sizeof(float));
cudaMalloc((void**)&dev_matB, N*N*sizeof(float));
cudaMalloc((void**)&dev_res, N*N*sizeof(float));

initMat(matA);
initMat(matB);
cudaMemcpy(dev_matA, matA, N*N*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(dev_matB, matB, N*N*sizeof(float), cudaMemcpyHostToDevice);

mat_add<<<numBlocks, threadsPerBlock>>>(dev_matA, dev_matB, dev_res);
cudaMemcpy(res, dev_res, N*N*sizeof(float), cudaMemcpyDeviceToHost);
cout<< endl << "###########parallel mat_add test######## ";
printRes();

mat_sub<<<numBlocks, threadsPerBlock>>>(dev_matA, dev_matB, dev_res);
cudaMemcpy(res, dev_res, N*N*sizeof(float), cudaMemcpyDeviceToHost);
cout<< endl << "###########parallel mat_add test######## ";
printRes();

mat_mult<<<numBlocks, threadsPerBlock>>>(dev_matA, dev_matB, dev_res);
cudaMemcpy(res, dev_res, N*N*sizeof(float), cudaMemcpyDeviceToHost);
cout<< endl << "###########parallel mat_mul test######## ";
printRes();

mat_add_serial(matA, matB, res);
cout<< endl << "###########serial mat_add test######## ";
printRes();

mat_sub_serial(matA, matB, res);
cout<< endl << "###########serial mat_add test######## ";
printRes();

mat_mult_serial(matA, matB, res);
cout<< endl << "###########serial mat_mul test######## ";
printRes();

int in;
cin >> in;
}
Loading