Skip to content
Snippets Groups Projects
Commit 01a086cb authored by Prakalp Srivastava's avatar Prakalp Srivastava
Browse files

linear-svm example committed to compute memory branch

parent c988910f
No related branches found
No related tags found
No related merge requests found
PARBOIL_ROOT = $(LLVM_SRC_ROOT)/test/VISC/parboil
APP = linear-svm
# Default compile visc
ifeq ($(VERSION),)
VERSION = visc_cm
endif
# Default use small test case
ifeq ($(TEST),)
TEST = small
endif
ifeq ($(PLATFORM),)
PLATFORM=default
endif
BIN = $(addsuffix -$(VERSION), $(APP))
SRCDIR = src/$(VERSION)
BUILDDIR = build/$(VERSION)_$(PLATFORM)
DATASET_DIR = $(PARBOIL_ROOT)/datasets/$(APP)
MATRIX1 = $(DATASET_DIR)/$(TEST)/input/matrix1.txt
MATRIX2 = $(DATASET_DIR)/$(TEST)/input/matrix2.txt
REF_OUTPUT = $(DATASET_DIR)/$(TEST)/output/matrix3.txt
RUNDIR = run/$(VERSION)/$(TEST)
OUTPUT = $(RUNDIR)/matrix3.txt
ARGS = -i $(MATRIX1),$(MATRIX2) -o $(OUTPUT)
TOOL = tools/compare-output
#TOOL=echo
include $(PARBOIL_ROOT)/common/mk/Makefile
This diff is collapsed.
# (c) 2010 The Board of Trustees of the University of Illinois.
LANGUAGE=visc
SRCDIR_OBJS=io.ll #compute_gold.o
VISC_OBJS=main.visc.ll
APP_CUDALDFLAGS=-lm -lstdc++
APP_CFLAGS=-ffast-math -O3
APP_CXXFLAGS=-ffast-math -O3
APP_OPTFLAGS=-unroll-threshold=300 -loop-unroll -scalarrepl
/***************************************************************************
*cr
*cr (C) Copyright 2010 The Board of Trustees of the
*cr University of Illinois
*cr All Rights Reserved
*cr
***************************************************************************/
/* I/O routines for reading and writing matrices in column-major
* layout
*/
#include<fstream>
#include<iostream>
#include<vector>
char* readFile(const char* fileName)
{
std::fstream f(fileName,std::fstream::in);
if(!f.good())
{
std::cerr<<"Error Reading File!!"<<std::endl;
return NULL;
}
f.seekg(0,std::ios::end);
int length = f.tellg();
f.seekg(0,std::ios::beg);
char* buffer;
if(length>0)
{
buffer = new char[length];
f.read(buffer,length);
buffer[length-1]=0;
}
else
{
buffer = new char;
buffer[0] = 0;
}
f.close();
return buffer;
}
bool readColMajorMatrixFile(const char *fn, int &nr_row, int &nr_col, std::vector<float>&v)
{
std::cerr << "Opening file:"<< fn << std::endl;
std::fstream f(fn, std::fstream::in);
if ( !f.good() ) {
return false;
}
// Read # of rows and cols
f >> nr_row;
f >> nr_col;
float data;
std::cerr << "Matrix dimension: "<<nr_row<<"x"<<nr_col<<std::endl;
while (f.good() ) {
f >> data;
v.push_back(data);
}
v.pop_back(); // remove the duplicated last element
return true;
}
bool writeColMajorMatrixFile(const char *fn, int nr_row, int nr_col, std::vector<float>&v)
{
std::cerr << "Opening file:"<< fn << " for write." << std::endl;
std::fstream f(fn, std::fstream::out);
if ( !f.good() ) {
return false;
}
// Read # of rows and cols
f << nr_row << " "<<nr_col<<" ";
float data;
std::cerr << "Matrix dimension: "<<nr_row<<"x"<<nr_col<<std::endl;
for (int i = 0; i < v.size(); ++i) {
f << v[i] << ' ';
}
f << "\n";
return true;
}
/***************************************************************************
*cr
*cr (C) Copyright 2010 The Board of Trustees of the
*cr University of Illinois
*cr All Rights Reserved
*cr
***************************************************************************/
/*
* Main entry of dense matrix-matrix multiplication kernel
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>
#include <malloc.h>
#include <vector>
#include <iostream>
#include <parboil.h>
#include <visc.h>
// I/O routines
extern bool readColMajorMatrixFile(const char *fn, int &nr_row, int &nr_col, std::vector<float>&v);
extern bool writeColMajorMatrixFile(const char *fn, int, int, std::vector<float>&);
extern char* readFile(const char*);
typedef struct __attribute__((__packed__)) {
float *X;
size_t bytesX;
float *W;
size_t bytesW;
float *Y;
size_t bytesY;
int nrows;
}
RootIn;
void packData(RootIn* args,
float *X, size_t bytesX,
float *W, size_t bytesW,
float *Y, size_t bytesY,
int nrows
) {
args->X = X;
args->bytesX = bytesX;
args->W = W;
args->bytesW = bytesW;
args->Y = Y;
args->bytesY = bytesY;
args->nrows = nrows;
}
void LinearLeaf( float* X, size_t bytesX, float* W, size_t bytesW, float* Y, size_t bytesY)
{
__visc__hint(visc::DEVICE);
__visc__attributes(2, X, W, 1, Y);
void* thisNode = __visc__getNode();
int rowID = __visc__getNodeInstanceID_x(thisNode);
int length = __visc__getVectorLength();
int offsetX = rowID*length;
float temp[length];
__visc__vector_mul(X+offsetX, W, temp, length);
Y[rowID] = __visc__reduction_sum(temp, length);
}
// Root node for linear SVM
void LinearRoot(float *X, size_t bytesX,
float *W, size_t bytesW,
float *Y, size_t bytesY,
int nrows
) {
__visc__hint(visc::DEVICE);
__visc__attributes(2, X, W, 1, Y);
void* LinearLeafNode = __visc__createNode1D(LinearLeaf, nrows);
// Bind edges
__visc__bindIn(LinearLeafNode, 0, 0, 0); // Bind X
__visc__bindIn(LinearLeafNode, 1, 1, 0); // Bind bytesX
__visc__bindIn(LinearLeafNode, 2, 2, 0); // Bind W
__visc__bindIn(LinearLeafNode, 3, 3, 0); // Bind bytesW
__visc__bindIn(LinearLeafNode, 4, 4, 0); // Bind Y
__visc__bindIn(LinearLeafNode, 5, 5, 0); // Bind bytesY
}
int main (int argc, char *argv[]) {
struct pb_Parameters *params;
struct pb_TimerSet timers;
size_t X_sz, Y_sz, W_sz;
int Xrow, Xcol, Wrow, Wcol;
std::vector<float> X, W;
/* Read command line. Expect 2 inputs: X, W
in column-major layout*/
params = pb_ReadParameters(&argc, argv);
if ((params->inpFiles[0] == NULL)
|| (params->inpFiles[1] == NULL)
|| (params->inpFiles[2] != NULL))
{
fprintf(stderr, "Expecting two input filenames\n");
exit(-1);
}
/* Read in data */
// load X
readColMajorMatrixFile(params->inpFiles[0],
Xrow, Xcol, X);
// load W
readColMajorMatrixFile(params->inpFiles[1],
Wrow, Wcol, W);
assert (Xcol == Wcol && "Width of X and W should be equal");
assert (Wrow == 1 && "Number of rows of W should be 1");
pb_InitializeTimerSet(&timers);
__visc__init();
pb_SwitchToTimer( &timers, pb_TimerID_COMPUTE );
X_sz = Xrow*Xcol*sizeof(float);
W_sz = Wcol*sizeof(float);
Y_sz = Xrow*sizeof(float);
std::vector<float> Y(Xrow);
llvm_visc_track_mem(&X.front(), X_sz);
llvm_visc_track_mem(&W.front(), W_sz);
llvm_visc_track_mem(&Y.front(), Y_sz);
pb_SwitchToTimer( &timers, pb_TimerID_COMPUTE );
for(size_t i=0; i<Y.size(); i++)
Y[i] = 0.0f;
// Pack data in struct
RootIn* args = (RootIn*) malloc(sizeof(RootIn));
packData(args,
&X.front(), X_sz,
&W.front(), W_sz,
&Y.front(), Y_sz,
Xrow
);
pb_SwitchToTimer(&timers, visc_TimerID_COMPUTATION );
void* linearDFG = __visc__launch(0, LinearRoot, (void*) args);
__visc__wait(linearDFG);
pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE );
pb_SwitchToTimer( &timers, pb_TimerID_COPY );
llvm_visc_request_mem(&Y.front(), Y_sz);
pb_SwitchToTimer( &timers, pb_TimerID_COMPUTE );
llvm_visc_untrack_mem(&X.front());
llvm_visc_untrack_mem(&W.front());
llvm_visc_untrack_mem(&Y.front());
pb_SwitchToTimer(&timers, pb_TimerID_NONE);
pb_PrintTimerSet(&timers);
__visc__cleanup();
if (params->outFile) {
/* Write Y to file */
//pb_SwitchToTimer(&timers, pb_TimerID_IO);
writeColMajorMatrixFile(params->outFile,
Xrow, 1, Y);
}
pb_FreeParameters(params);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment