Skip to content

Commit

Permalink
test group 0
Browse files Browse the repository at this point in the history
  • Loading branch information
antinucleon committed Dec 29, 2014
1 parent de6826c commit 86bbf1e
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# set LD_LIBRARY_PATH
export CC = gcc
export CXX = g++
export NVCC =nvcc
export CFLAGS = -Wall -O3 -g -msse3 -Wno-unknown-pragmas -funroll-loops -I../
export LDFLAGS= -g -lm -lcublas -lcudart
export NVCCFLAGS = -O3 --use_fast_math -ccbin $(CXX)

# specify tensor path
BIN =
OBJ =
CUOBJ =
CUBIN = test
.PHONY: clean all

all: $(CUBIN) $(OBJ)

test: test.cu

$(BIN) :
$(CXX) $(CFLAGS) -o $@ $(filter %.cpp %.o %.c, $^) $(LDFLAGS)

$(OBJ) :
$(CXX) -c $(CFLAGS) -o $@ $(firstword $(filter %.cpp %.c, $^) )

$(CUOBJ) :
$(NVCC) -c -o $@ $(NVCCFLAGS) -Xcompiler "$(CFLAGS)" $(filter %.cu, $^)

$(CUBIN) :
$(NVCC) -o $@ $(NVCCFLAGS) -Xcompiler "$(CFLAGS)" -Xlinker "$(LDFLAGS)" $(filter %.cu %.cpp %.o, $^)

clean:
$(RM) $(OBJ) $(BIN) $(CUBIN) $(CUOBJ) *~
64 changes: 64 additions & 0 deletions test/test.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "test.h"

using namespace mshadow;


int main() {
InitTensorEngine();
Tensor<cpu, 3, float> tc = NewTensor<cpu, float>(Shape3(3, 2, 4), 0.0f);
Tensor<gpu, 3, float> tg = NewTensor<gpu, float>(tc.shape_, 0.0f);
// init
for (index_t i = 0; i < tc.size(0); ++i) {
for (index_t j = 0; j < tc.size(1); ++j) {
for (index_t k = 0; k < tc.size(2); ++k) {
tc[i][j][k] = i * 0.1f + j * 0.2f + k * 0.1f;
}
}
}
Copy(tg, tc);
// print
printf("\n#print batch 0 of cpu tensor:\n");
Print2DTensor(tc[0]);
// check
if (Check2DTensor(tg[1], tc[1])) {
printf("batch 1 of gpu & cpu tensor are same.\n");
}
// sum of row
Tensor<cpu, 1, float> tmp_tc = NewTensor<cpu, float>(Shape1(tc[0].size(1)), 0.0f);
Tensor<gpu, 1, float> tmp_tg = NewTensor<gpu, float>(Shape1(tg[0].size(1)), 0.0f);
printf("\n#sum_rows of batch 0:\n");
tmp_tc = sum_rows(tc[0]);
tmp_tg = sum_rows(tg[0]);
Print1DTensor(tmp_tc);
if (Check1DTensor(tmp_tg, tmp_tc)) {
printf("cpu & gpu result consists\n");
}
FreeSpace(&tmp_tc);
FreeSpace(&tmp_tg);
// sumall_except_dim: Waring, random fail!
printf("\n#sumall_except_dim<0> of batch 0:\n");
Tensor<cpu, 1, float> red_tc = NewTensor<cpu, float>(Shape1(2), 0.0f);
Tensor<gpu, 1, float> red_tg = NewTensor<gpu, float>(Shape1(2), 0.0f);
red_tc = sumall_except_dim<0>(tc[0]);
red_tg = sumall_except_dim<0>(tg[0]);
Print1DTensor(red_tc);
if (Check1DTensor(red_tg, red_tc)) {
printf("cpu & gpu result consists\n");
}
FreeSpace(&red_tc);
FreeSpace(&red_tg);
// softmax
printf("\n#Softmax\n");
Tensor<cpu, 2, float> sm_tc = NewTensor<cpu, float>(tc[0].shape_, 0.0f);
Tensor<gpu, 2, float> sm_tg = NewTensor<gpu, float>(tg[0].shape_, 0.0f);
Softmax(sm_tc, tc[0]);
Softmax(sm_tg, tg[0]);
FreeSpace(&sm_tc);
FreeSpace(&sm_tg);
if (Check2DTensor(sm_tg, sm_tc)) {
printf("cpu & gpu result consists\n");
}
FreeSpace(&tc);
FreeSpace(&tg);
ShutdownTensorEngine();
}
67 changes: 67 additions & 0 deletions test/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef TEST_H
#define TEST_H

#include "mshadow/tensor.h"
#include "assert.h"

#define EPS 0.0001
using namespace mshadow;
using namespace mshadow::expr;


template<typename xpu>
void Print2DTensor(Tensor<xpu, 2, float> const &ts);

template<typename xpu>
void Print1DTensor(Tensor<xpu, 1, float> const &ts);

template<>
void Print1DTensor(Tensor<cpu, 1, float> const &ts) {
for (index_t i = 0; i < ts.size(0); ++i) {
printf("%.2f ", ts[i]);
}
printf("\n");
}


template<>
void Print2DTensor(Tensor<cpu, 2, float> const &ts) {
for (index_t i = 0; i < ts.size(0); ++i) {
Print1DTensor(ts[i]);
}
}

template<>
void Print2DTensor(Tensor<gpu, 2, float> const &tg) {
Tensor<cpu, 2, float> tc = NewTensor<cpu, float>(tg.shape_, 0.0f);
Copy(tc, tg);
Print2DTensor(tc);
FreeSpace(&tc);
}



bool Check2DTensor(Tensor<gpu, 2, float> const &tg, Tensor<cpu, 2, float> const &tc) {
Tensor<cpu, 2, float> tcc = NewTensor<cpu, float>(tg.shape_, 0.0f);
Copy(tcc, tg);
for (index_t i = 0; i < tc.size(0); ++i) {
for (index_t j = 0; j < tc.size(1); ++j) {
assert(abs(tcc[i][j] - tc[i][j]) < EPS);
}
}
FreeSpace(&tcc);
return true;
}

bool Check1DTensor(Tensor<gpu, 1, float> const &tg, Tensor<cpu, 1, float> const &tc) {
Tensor<cpu, 1, float> tcc = NewTensor<cpu, float>(tc.shape_, 0.0f);
Copy(tcc, tg);
// printf("gpu result:\n");
// Print1DTensor(tcc);
for (index_t i = 0; i < tc.size(0); ++i) {
assert(abs(tcc[i] - tc[i]) < EPS);
}
FreeSpace(&tcc);
return true;
}
#endif

0 comments on commit 86bbf1e

Please sign in to comment.