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

Nathan Marshak: CUDA-Test works on Fedora 18. #20

Open
wants to merge 3 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
10 changes: 10 additions & 0 deletions FEDORA_18_HOWTO
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Here's what I had to do to get this homework assignment to build on Fedora 18:

All the steps, based on http://www.r-tutor.com/gpu-computing/cuda-installation/cuda5.5-fc18:

1. Uninstall the Nvidia drivers that I had installed from the RPMFusion repository.
2. Install the Nvidia drivers from the CUDA repository. The installer will fail to build the Nvidia Kernel modules because Fedora 18 currently runs kernel 3.10. Therefore, I had to apply this patch: http://pastebin.com/N0a5KMZa then manually execute the rest of the install scripts inside the RPM package.
3. Install CUDA per the tutorial in the link.
4. I couldn't get GLFW to work, so I used the Windows version as a starting point instead of the Mac Version :-P
5. I customized the Mac version's makefile to get it to compile.
6. I couldn't get Git to authenticate over HTTPS for some bizarre reason. I did it through SSH, by generating keys first: https://help.github.com/articles/generating-ssh-keys
Binary file added PROJ0_LINUX/CUDA_test_screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PROJ0_LINUX/bin/CUDATEST
Binary file not shown.
2 changes: 2 additions & 0 deletions PROJ0_LINUX/bin/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CC = /usr/bin/gcc -m64
NVCC = $(CUDA_HOME)/bin/nvcc -m64
30 changes: 30 additions & 0 deletions PROJ0_LINUX/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
NVCC = $(CUDA_HOME)/bin/nvcc -m64
CC = /usr/bin/gcc -m64

#GLFW_INCLUDE_PATH = -Iglfw/include/
#GLFW_LIB_PATH = -Lglfw/lib/
#GLFW_INCLUDE_PATH = -Iglfw
#GLFW_LIB_PATH = -Lglfw
GLFW = $(GLFW_INCLUDE_PATH) $(GLFW_LIB_PATH)

CUDA_SAMPLES = -I$(CUDA_HOME)/samples/common/inc
CUDA_INCLUDE = -I/usr/local/cuda/include
CUDASDK_C_LIB_PATH = -L/Developer/GPU\ Computing/C/lib
CUDASDK_C_INCLUDE_PATH = -I/Developer/GPU\ Computing/C/common/inc
CUDA = $(CUDA_INCLUDE) $(CUDASDK_C_LIB_PATH) $(CUDASDK_C_INCLUDE_PATH) $(CUDA_SAMPLES)

#XLINKER = -Xlinker -framework,OpenGL,-framework,GLUT
LIBRARIES = -lglut -lGL -lGLU -lGLEW

LFLAGS = $(GLFW) $(CUDA) $(LIBRARIES) -lglfw

all: CUDATEST

CUDATEST: src/main.cpp
$(NVCC) $(GLEW_PATH) $(LFLAGS) src/main.cpp src/kernel.cu src/glslUtility.cpp -o bin/CUDATEST

clean:
rm bin/CUDATEST
rm *.o

.PHONY : bin/CUDATEST
3 changes: 3 additions & 0 deletions PROJ0_LINUX/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
export DYLD_LIBRARY_PATH='/usr/local/cuda/lib:glfw/lib';
./bin/CUDATEST;
8 changes: 8 additions & 0 deletions PROJ0_LINUX/shaders/passthroughFS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
varying vec2 v_Texcoords;

uniform sampler2D u_image;

void main(void)
{
gl_FragColor = texture2D(u_image, v_Texcoords);
}
9 changes: 9 additions & 0 deletions PROJ0_LINUX/shaders/passthroughVS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
attribute vec4 Position;
attribute vec2 Texcoords;
varying vec2 v_Texcoords;

void main(void)
{
v_Texcoords = Texcoords;
gl_Position = Position;
}
154 changes: 154 additions & 0 deletions PROJ0_LINUX/src/glslUtility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// GLSL Utility: A utility class for loading GLSL shaders, for Patrick Cozzi's CIS565: GPU Computing at the University of Pennsylvania
// Written by Varun Sampath and Patrick Cozzi, Copyright (c) 2012 University of Pennsylvania

#include "glslUtility.h"

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using std::ios;

namespace glslUtility {

typedef struct {
GLuint vertex;
GLuint fragment;
} shaders_t;

char* loadFile(const char *fname, GLint &fSize)
{
// file read based on example in cplusplus.com tutorial
std::ifstream file (fname, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
unsigned int size = (unsigned int)file.tellg();
fSize = size;
char *memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
//std::cout << "file " << fname << " loaded" << std::endl;
return memblock;
}

std::cout << "Unable to open file " << fname << std::endl;
exit(1);
}

// printShaderInfoLog
// From OpenGL Shading Language 3rd Edition, p215-216
// Display (hopefully) useful error messages if shader fails to compile
void printShaderInfoLog(GLint shader)
{
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;

glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLen);

if (infoLogLen > 1)
{
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetShaderInfoLog(shader,infoLogLen, &charsWritten, infoLog);
//std::cout << "InfoLog:" << std::endl << infoLog << std::endl;
delete [] infoLog;
}
}

void printLinkInfoLog(GLint prog)
{
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;

glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &infoLogLen);

if (infoLogLen > 1)
{
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetProgramInfoLog(prog,infoLogLen, &charsWritten, infoLog);
//std::cout << "InfoLog:" << std::endl << infoLog << std::endl;
delete [] infoLog;
}
}

shaders_t loadShaders(const char * vert_path, const char * frag_path) {
GLuint f, v;

char *vs,*fs;

v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);

// load shaders & get length of each
GLint vlen;
GLint flen;
vs = loadFile(vert_path,vlen);
fs = loadFile(frag_path,flen);

const char * vv = vs;
const char * ff = fs;

glShaderSource(v, 1, &vv,&vlen);
glShaderSource(f, 1, &ff,&flen);

GLint compiled;

glCompileShader(v);
glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
std::cout << "Vertex shader not compiled." << std::endl;
}
printShaderInfoLog(v);

glCompileShader(f);
glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
std::cout << "Fragment shader not compiled." << std::endl;
}
printShaderInfoLog(f);

shaders_t out; out.vertex = v; out.fragment = f;

delete [] vs; // dont forget to free allocated memory, or else really bad things start happening
delete [] fs; // we allocated this in the loadFile function...

return out;
}

void attachAndLinkProgram( GLuint program, shaders_t shaders) {
glAttachShader(program, shaders.vertex);
glAttachShader(program, shaders.fragment);

glLinkProgram(program);
GLint linked;
glGetProgramiv(program,GL_LINK_STATUS, &linked);
if (!linked)
{
std::cout << "Program did not link." << std::endl;
}
printLinkInfoLog(program);
}

GLuint createProgram(const char *vertexShaderPath, const char *fragmentShaderPath, const char *attributeLocations[], GLuint numberOfLocations)
{
glslUtility::shaders_t shaders = glslUtility::loadShaders(vertexShaderPath, fragmentShaderPath);

GLuint program = glCreateProgram();

for (GLuint i = 0; i < numberOfLocations; ++i)
{
glBindAttribLocation(program, i, attributeLocations[i]);
}

glslUtility::attachAndLinkProgram(program, shaders);

return program;
}
}
16 changes: 16 additions & 0 deletions PROJ0_LINUX/src/glslUtility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// GLSL Utility: A utility class for loading GLSL shaders, for Patrick Cozzi's CIS565: GPU Computing at the University of Pennsylvania
// Written by Varun Sampath and Patrick Cozzi, Copyright (c) 2012 University of Pennsylvania

#ifndef GLSLUTILITY_H_
#define GLSLUTILITY_H_

#include <GL/glew.h>

namespace glslUtility
{

GLuint createProgram(const char *vertexShaderPath, const char *fragmentShaderPath, const char *attributeLocations[], GLuint numberOfLocations);

}

#endif
75 changes: 75 additions & 0 deletions PROJ0_LINUX/src/kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// CIS565 CUDA Checker: A simple CUDA hello-world style program for Patrick Cozzi's CIS565: GPU Computing at the University of Pennsylvania
// Written by Yining Karl Li, Copyright (c) 2012 University of Pennsylvania
// This file includes code from:
// Rob Farber for CUDA-GL interop, from CUDA Supercomputing For The Masses: http://www.drdobbs.com/architecture-and-design/cuda-supercomputing-for-the-masses-part/222600097

#include <stdio.h>
#include <cuda.h>
#include <cmath>
//#include <cutil_math.h>
#include "kernel.h"
#include <iostream>

void checkCUDAError(const char *msg) {
cudaError_t err = cudaGetLastError();
if( cudaSuccess != err) {
fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) );
exit(EXIT_FAILURE);
}
}

//Kernel that writes the image to the OpenGL PBO directly.
__global__ void createVersionVisualization(uchar4* PBOpos, int width, int height, int major, int minor){

int x = (blockIdx.x * blockDim.x) + threadIdx.x;
int y = (blockIdx.y * blockDim.y) + threadIdx.y;
int index = x + (y * width);

if(x<=width && y<=height){
// Each thread writes one pixel location in the texture (textel)
PBOpos[index].w = 0;
PBOpos[index].x = 0;
PBOpos[index].y = 0;
PBOpos[index].z = 0;

if(y<height/2){
if(major==1){
PBOpos[index].x = 255;
}else if(major==2){
PBOpos[index].y = 255;
}else if(major==3){
PBOpos[index].z = 255;
}
}else{
if(minor==0){
PBOpos[index].x = 255;
}else if(minor==1){
PBOpos[index].y = 255;
}else if(minor==2){
PBOpos[index].z = 255;
}else if(minor==3){
PBOpos[index].x = 255;
PBOpos[index].y = 255;
}else if(minor==5){
PBOpos[index].z = 255;
PBOpos[index].y = 255;
}
}
}
}

// Wrapper for the __global__ call that sets up the kernel calls and does a ton of memory management
void cudaKernel(uchar4* PBOpos, int width, int height, int major, int minor){

// set up crucial magic
int tileSize = 16;
dim3 threadsPerBlock(tileSize, tileSize);
dim3 fullBlocksPerGrid((int)ceil(width/float(tileSize)), (int)ceil(height/float(tileSize)));

//kernel launches
createVersionVisualization<<<fullBlocksPerGrid, threadsPerBlock>>>(PBOpos, width, height, major, minor);
// make certain the kernel has completed
cudaThreadSynchronize();

checkCUDAError("Kernel failed!");
}
20 changes: 20 additions & 0 deletions PROJ0_LINUX/src/kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// CIS565 CUDA Checker: A simple CUDA hello-world style program for Patrick Cozzi's CIS565: GPU Computing at the University of Pennsylvania
// Written by Yining Karl Li, Copyright (c) 2012 University of Pennsylvania
// This file includes code from:
// Rob Farber for CUDA-GL interop, from CUDA Supercomputing For The Masses: http://www.drdobbs.com/architecture-and-design/cuda-supercomputing-for-the-masses-part/222600097

#ifndef KERNEL_H
#define KERNEL_H

#include <cuda.h>
#include <cmath>

#if CUDA_VERSION >= 5000
#include <helper_math.h>
#else
#include <cutil_math.h>
#endif

void cudaKernel(uchar4* pos, int width, int height, int major, int minor);

#endif //KERNEL_H
Loading