Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
[ocl/nn] measure the time
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhc committed Sep 5, 2017
1 parent 5e87bf9 commit 21537d2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
26 changes: 22 additions & 4 deletions opencl/nn/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
include ../../common/make.config

#Can be changed by `make TYPE=CPU`
TYPE = GPU

#Library
ifeq ($(TYPE),GPU)
OPENCL_INC = $(NV_OPENCL_INC)
OPENCL_LIB = $(NV_OPENCL_LIB)
else
OPENCL_INC = $(INTEL_OPENCL_INC)
OPENCL_LIB = $(INTEL_OPENCL_LIB)
endif

#C compiler
CC = g++

Expand All @@ -10,7 +22,13 @@ CC_FLAGS = -g -O3
EXE = nn.out

release:
$(CC) $(CC_FLAGS) -fopenmp $(SRC) -o $(EXE) -I$(OPENCL_INC) -L$(OPENCL_LIB) -lOpenCL

clean:
rm -f $(EXE)
$(CC) $(CC_FLAGS) -fopenmp $(SRC) -o $(EXE) \
-I$(OPENCL_INC) -L$(OPENCL_LIB) -lOpenCL \
-I../util -DTIMING \
-Wno-unused-result

clean:
rm -f $(EXE)

run: release
./$(EXE) filelist.txt -p 0 -d 0 -r 50 -lat 30 -lng 90 -q -t
62 changes: 60 additions & 2 deletions opencl/nn/nearestNeighbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
#define __NEAREST_NEIGHBOR__

#include "nearestNeighbor.h"
#include "timing.h"

#ifdef TIMING
#define PROFILING

struct timeval tv;
struct timeval tv_total_start, tv_total_end;
struct timeval tv_init_end;
struct timeval tv_h2d_start, tv_h2d_end;
struct timeval tv_d2h_start, tv_d2h_end;
struct timeval tv_kernel_start, tv_kernel_end;
struct timeval tv_mem_alloc_start, tv_mem_alloc_end;
struct timeval tv_close_start, tv_close_end;
float init_time = 0, mem_alloc_time = 0, h2d_time = 0, kernel_time = 0,
d2h_time = 0, close_time = 0, total_time = 0;
#endif

cl_context context=NULL;

Expand Down Expand Up @@ -36,8 +52,16 @@ int main(int argc, char *argv[]) {

if (resultsCount > numRecords) resultsCount = numRecords;

#ifdef TIMING
gettimeofday(&tv_total_start, NULL);
#endif
context = cl_init_context(platform,device,quiet);

#ifdef TIMING
gettimeofday(&tv_init_end, NULL);
tvsub(&tv_init_end, &tv_total_start, &tv);
init_time = tv.tv_sec * 1000.0 + (float) tv.tv_usec / 1000.0;
#endif

recordDistances = OpenClFindNearestNeighbors(context,numRecords,locations,lat,lng,timing);

// find the resultsCount least distances
Expand Down Expand Up @@ -77,11 +101,19 @@ float *OpenClFindNearestNeighbors(

cl_int error=0;

#ifdef TIMING
gettimeofday(&tv_mem_alloc_start, NULL);
#endif
d_locations = clCreateBuffer(context, CL_MEM_READ_ONLY,
sizeof(LatLong) * numRecords, NULL, &error);

d_distances = clCreateBuffer(context, CL_MEM_READ_WRITE,
sizeof(float) * numRecords, NULL, &error);
#ifdef TIMING
gettimeofday(&tv_mem_alloc_end, NULL);
tvsub(&tv_mem_alloc_end, &tv_mem_alloc_start, &tv);
mem_alloc_time = tv.tv_sec * 1000.0 + (float) tv.tv_usec / 1000.0;
#endif

cl_command_queue command_queue = cl_getCommandQueue();
cl_event writeEvent,kernelEvent,readEvent;
Expand Down Expand Up @@ -135,6 +167,7 @@ float *OpenClFindNearestNeighbors(

cl_errChk(error,"ERROR with clEnqueueReadBuffer",true);
if (timing) {
#ifdef TIMING
clFinish(command_queue);
cl_ulong eventStart,eventEnd,totalTime=0;
printf("# Records\tWrite(s) [size]\t\tKernel(s)\tRead(s) [size]\t\tTotal(s)\n");
Expand All @@ -149,6 +182,8 @@ float *OpenClFindNearestNeighbors(

printf("%f [%.2fMB]\t",(float)((eventEnd-eventStart)/1e9),(float)((sizeof(LatLong) * numRecords)/1e6));
totalTime += eventEnd-eventStart;
h2d_time = (eventEnd - eventStart) / 1e6;

// Kernel
error = clGetEventProfilingInfo(kernelEvent,CL_PROFILING_COMMAND_START,
sizeof(cl_ulong),&eventStart,NULL);
Expand All @@ -159,6 +194,8 @@ float *OpenClFindNearestNeighbors(

printf("%f\t",(float)((eventEnd-eventStart)/1e9));
totalTime += eventEnd-eventStart;
kernel_time = (eventEnd - eventStart) / 1e6;

// Read Buffer
error = clGetEventProfilingInfo(readEvent,CL_PROFILING_COMMAND_START,
sizeof(cl_ulong),&eventStart,NULL);
Expand All @@ -169,12 +206,33 @@ float *OpenClFindNearestNeighbors(

printf("%f [%.2fMB]\t",(float)((eventEnd-eventStart)/1e9),(float)((sizeof(float) * numRecords)/1e6));
totalTime += eventEnd-eventStart;

d2h_time = (eventEnd - eventStart) / 1e6;

printf("%f\n\n",(float)(totalTime/1e9));
#endif
}

// 6. return finalized data and release buffers
#ifdef TIMING
gettimeofday(&tv_close_start, NULL);
#endif
clReleaseMemObject(d_locations);
clReleaseMemObject(d_distances);
#ifdef TIMING
gettimeofday(&tv_close_end, NULL);
tvsub(&tv_close_end, &tv_close_start, &tv);
close_time = tv.tv_sec * 1000.0 + (float) tv.tv_usec / 1000.0;
tvsub(&tv_close_end, &tv_total_start, &tv);
total_time = tv.tv_sec * 1000.0 + (float) tv.tv_usec / 1000.0;

printf("Init: %f\n", init_time);
printf("MemAlloc: %f\n", mem_alloc_time);
printf("HtoD: %f\n", h2d_time);
printf("Exec: %f\n", kernel_time);
printf("DtoH: %f\n", d2h_time);
printf("Close: %f\n", close_time);
printf("Total: %f\n", total_time);
#endif
return distances;
}

Expand Down

0 comments on commit 21537d2

Please sign in to comment.