Browse Source
Merge branch 'master' of 192.168.200.125:/srv/git/dr_frucht
Merge branch 'master' of 192.168.200.125:/srv/git/dr_frucht
63 changed files with 3114 additions and 252 deletions
-
20Projects/Cortex_test/cortex_test.sln
-
40Projects/Cortex_test/cortex_test/ReadMe.txt
-
70Projects/Cortex_test/cortex_test/cortex_test.cpp
-
8Projects/Cortex_test/cortex_test/stdafx.cpp
-
30Projects/Cortex_test/cortex_test/stdafx.h
-
8Projects/Cortex_test/cortex_test/targetver.h
-
254lib/data/cortex/base/basicCortex.cpp
-
30lib/data/cortex/base/basicCortex.h
-
37lib/data/cortex/base/basicImage.cpp
-
11lib/data/cortex/base/basicImage.h
-
14lib/data/cortex/base/basicLayer.cpp
-
50lib/data/cortex/base/basicLayer.h
-
7lib/data/cortex/base/basicList.cpp
-
2lib/data/cortex/base/basicList.h
-
163lib/data/cortex/base/basicProcessingUnit.cpp
-
24lib/data/cortex/base/basicProcessingUnit.h
-
4lib/data/cortex/cpu/cpuBatchnormLayer.cpp
-
28lib/data/cortex/cpu/cpuConvolutionalLayer.cpp
-
6lib/data/cortex/cuda/col2im_kernels.cu
-
4lib/data/cortex/cuda/convolutional_kernels.cu
-
252lib/data/cortex/cuda/cuda.cu
-
53lib/data/cortex/cuda/cudaAvgpoolLayer.cu
-
4lib/data/cortex/cuda/cudaBatchnormLayer.cpp
-
121lib/data/cortex/cuda/cudaCalcs.cu
-
249lib/data/cortex/cuda/cudaCropLayer.cu
-
47lib/data/cortex/cuda/cudaDropoutLayer.cu
-
614lib/data/cortex/cuda/cudaMakers.cu
-
121lib/data/cortex/cuda/cudaMaxpoolLayer.cu
-
6lib/data/cortex/cuda/im2col_kernels.cu
-
97lib/data/cortex/cuda/network_kernels.cu
-
64lib/data/cortex/layers/batchnormLayer.cpp
-
43lib/data/cortex/layers/batchnormLayer.h
-
19lib/data/cortex/layers/connectedLayer.cpp
-
1lib/data/cortex/layers/connectedLayer.h
-
89lib/data/cortex/layers/convolutionalLayer.cpp
-
1lib/data/cortex/layers/convolutionalLayer.h
-
6lib/data/cortex/layers/crnnLayer.cpp
-
1lib/data/cortex/layers/crnnLayer.h
-
7lib/data/cortex/layers/detectionLayer.cpp
-
3lib/data/cortex/layers/detectionLayer.h
-
15lib/data/cortex/layers/gruLayer.cpp
-
1lib/data/cortex/layers/gruLayer.h
-
28lib/data/cortex/layers/layersFactory.cpp
-
7lib/data/cortex/layers/layersFactory.h
-
12lib/data/cortex/layers/localLayer.cpp
-
1lib/data/cortex/layers/localLayer.h
-
3lib/data/cortex/layers/maxpoolLayer.cpp
-
9lib/data/cortex/layers/regionLayer.cpp
-
2lib/data/cortex/layers/regionLayer.h
-
6lib/data/cortex/layers/rnnLayer.cpp
-
1lib/data/cortex/layers/rnnLayer.h
-
105lib/sdl/core/coreLabel.cpp
-
43lib/sdl/core/coreLabel.h
-
32lib/sdl/core/coreStyleTemplate.h
-
53lib/sdl/core/coreWidget.cpp
-
33lib/sdl/core/coreWidget.h
-
193lib/sdl/gui_sdl.cpp
-
42lib/sdl/gui_sdl.h
-
59lib/sdl/widgets/logWidget.cpp
-
29lib/sdl/widgets/logWidget.h
-
59lib/sdl/widgets/progressBar.cpp
-
23lib/sdl/widgets/progressBar.h
-
2put_to_git
@ -0,0 +1,20 @@ |
|||
|
|||
Microsoft Visual Studio Solution File, Format Version 11.00 |
|||
# Visual Studio 2010 |
|||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cortex_test", "cortex_test\cortex_test.vcxproj", "{CB0601AE-A104-40CF-83D1-DA66736E8322}" |
|||
EndProject |
|||
Global |
|||
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
|||
Debug|Win32 = Debug|Win32 |
|||
Release|Win32 = Release|Win32 |
|||
EndGlobalSection |
|||
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
|||
{CB0601AE-A104-40CF-83D1-DA66736E8322}.Debug|Win32.ActiveCfg = Debug|Win32 |
|||
{CB0601AE-A104-40CF-83D1-DA66736E8322}.Debug|Win32.Build.0 = Debug|Win32 |
|||
{CB0601AE-A104-40CF-83D1-DA66736E8322}.Release|Win32.ActiveCfg = Release|Win32 |
|||
{CB0601AE-A104-40CF-83D1-DA66736E8322}.Release|Win32.Build.0 = Release|Win32 |
|||
EndGlobalSection |
|||
GlobalSection(SolutionProperties) = preSolution |
|||
HideSolutionNode = FALSE |
|||
EndGlobalSection |
|||
EndGlobal |
|||
@ -0,0 +1,40 @@ |
|||
======================================================================== |
|||
CONSOLE APPLICATION : cortex_test Project Overview |
|||
======================================================================== |
|||
|
|||
AppWizard has created this cortex_test application for you. |
|||
|
|||
This file contains a summary of what you will find in each of the files that |
|||
make up your cortex_test application. |
|||
|
|||
|
|||
cortex_test.vcxproj |
|||
This is the main project file for VC++ projects generated using an Application Wizard. |
|||
It contains information about the version of Visual C++ that generated the file, and |
|||
information about the platforms, configurations, and project features selected with the |
|||
Application Wizard. |
|||
|
|||
cortex_test.vcxproj.filters |
|||
This is the filters file for VC++ projects generated using an Application Wizard. |
|||
It contains information about the association between the files in your project |
|||
and the filters. This association is used in the IDE to show grouping of files with |
|||
similar extensions under a specific node (for e.g. ".cpp" files are associated with the |
|||
"Source Files" filter). |
|||
|
|||
cortex_test.cpp |
|||
This is the main application source file. |
|||
|
|||
///////////////////////////////////////////////////////////////////////////// |
|||
Other standard files: |
|||
|
|||
StdAfx.h, StdAfx.cpp |
|||
These files are used to build a precompiled header (PCH) file |
|||
named cortex_test.pch and a precompiled types file named StdAfx.obj. |
|||
|
|||
///////////////////////////////////////////////////////////////////////////// |
|||
Other notes: |
|||
|
|||
AppWizard uses "TODO:" comments to indicate parts of the source code you |
|||
should add to or customize. |
|||
|
|||
///////////////////////////////////////////////////////////////////////////// |
|||
@ -0,0 +1,70 @@ |
|||
// cortex_test.cpp : Defines the entry point for the console application.
|
|||
//
|
|||
|
|||
#include "stdafx.h"
|
|||
|
|||
|
|||
int _tmain(int argc, _TCHAR* argv[]){ |
|||
objectConsoleLoggingService CL; |
|||
cortexLayersFactory * mFACTORY; |
|||
classGUI_SDL * mGUI; |
|||
sdlProgressBar * mPBar; |
|||
sdlLog * mLog; |
|||
basicLogCollection::getInstance()->addHandler(&CL); |
|||
mGUI = classGUI_SDL::getInstance(); |
|||
mLog = new sdlLog( 10,300,600,200); |
|||
basicLogCollection::getInstance()->addHandler(mLog); |
|||
|
|||
mFACTORY = cortexLayersFactory::getInstance(); |
|||
mPBar = new sdlProgressBar(120, 120, 500, 40); |
|||
mPBar->attachStatusProvider(mFACTORY); |
|||
mFACTORY->setGpuUsageMode(false); |
|||
mFACTORY->setPushPullMode(true); |
|||
float detectionThreshold = 0.5f; |
|||
basicCortex * Net; |
|||
Net = mFACTORY->parseConfigCustom("C:\\FruchtSystems\\darknet-master\\cfg\\tiny-yolo.cfg", 1 ); |
|||
//Net = mFACTORY->parseConfig("C:\\FruchtSystems\\dn_vs13\\x64\\Debug\\cfg\\yolo-obj.cfg");
|
|||
//Net = mFACTORY->parseConfig("C:\\FruchtSystems\\darknet-master\\cfg\\darknet.cfg");
|
|||
if( Net != NULL ){ |
|||
//delete(mPBar);
|
|||
Net->loadWeights("C:\\FruchtSystems\\dn_vs13\\x64\\Debug\\yolo-obj.backup"); |
|||
Net->setBatch(1); |
|||
|
|||
regionLayer *RL; |
|||
RL = (regionLayer*)Net->getLayer(Net->getLayersCount() - 1); |
|||
|
|||
float nms = .4f; |
|||
uint32_t box_cnt; |
|||
uint32_t cls_cnt; |
|||
box_cnt = RL->getBoxesCount(); |
|||
cls_cnt = RL->getClassesCount(); |
|||
cortexBOXLABEL *boxes = (cortexBOXLABEL*)calloc(box_cnt, sizeof(cortexBOXLABEL)); |
|||
float **probs = (float**)calloc(box_cnt, sizeof(float*)); |
|||
for (uint32_t j = 0; j < box_cnt; ++j){ |
|||
memset(boxes, 0, box_cnt*sizeof(cortexBOXLABEL)); |
|||
probs[j] = (float*)calloc(cls_cnt, sizeof(float)); |
|||
memset(probs[j], 0, cls_cnt*sizeof(float)); |
|||
}; |
|||
cv::Mat img; |
|||
cv::Mat r_img; |
|||
char *tPath = "C:\\FruchtSystems\\darknet-master\\data\\dog.jpg"; |
|||
img = cv::imread(tPath, CV_LOAD_IMAGE_COLOR);//CV_LOAD_IMAGE_GRAYSCALE
|
|||
basicLogCollection::getInstance()->Log("Main", NULL, tPath ); |
|||
// Check for invalid input
|
|||
if( !img.data ){ |
|||
basicLogCollection::getInstance()->Log("Main", NULL, "Could not open or find the image!"); |
|||
} else { |
|||
cv::resize(img, r_img, cv::Size(Net->getInputWidth(), Net->getInputHeight()), 0, 0, CV_INTER_LINEAR); |
|||
cortexImage image(&r_img); |
|||
Net->predict(image.data); |
|||
RL->get_region_boxes(1, 1, detectionThreshold, probs, boxes, 0, 0 ); |
|||
|
|||
}; |
|||
mGUI->waitThreadEnd(); |
|||
delete(Net); |
|||
} else { |
|||
basicLogCollection::getInstance()->Log("Main", NULL, "Error Loading Network Configuration!"); |
|||
}; |
|||
return 0; |
|||
} |
|||
|
|||
@ -0,0 +1,8 @@ |
|||
// stdafx.cpp : source file that includes just the standard includes
|
|||
// cortex_test.pch will be the pre-compiled header
|
|||
// stdafx.obj will contain the pre-compiled type information
|
|||
|
|||
#include "stdafx.h"
|
|||
|
|||
// TODO: reference any additional headers you need in STDAFX.H
|
|||
// and not in this file
|
|||
@ -0,0 +1,30 @@ |
|||
// stdafx.h : include file for standard system include files, |
|||
// or project specific include files that are used frequently, but |
|||
// are changed infrequently |
|||
// |
|||
|
|||
#pragma once |
|||
|
|||
#include "targetver.h" |
|||
|
|||
#include <stdio.h> |
|||
#include <tchar.h> |
|||
|
|||
//#include <algorithm> |
|||
//#include <boost/chrono.hpp> |
|||
//#include <iostream> |
|||
//#include <boost/mmutex> |
|||
|
|||
//#include <vector> |
|||
|
|||
#include "../../lib/data/cortex/cpu/cpu.h" |
|||
#include "../../lib/data/cortex/cuda/cuda.h" |
|||
#include "../../lib/components/object/objectConsoleLoggingService.h" |
|||
#include "../../lib/data/cortex/layers/layersFactory.h" |
|||
#include "../../lib/data/cortex/layers/regionLayer.h" |
|||
//#include "../../lib/sdl/gui_sdl.h" |
|||
#include "../../lib/sdl/widgets/progressBar.h" |
|||
#include "../../lib/sdl/widgets/logWidget.h" |
|||
|
|||
|
|||
// TODO: reference additional headers your program requires here |
|||
@ -0,0 +1,8 @@ |
|||
#pragma once |
|||
|
|||
// Including SDKDDKVer.h defines the highest available Windows platform. |
|||
|
|||
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and |
|||
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. |
|||
|
|||
#include <SDKDDKVer.h> |
|||
@ -0,0 +1,252 @@ |
|||
#include <cuda_runtime.h> |
|||
#include <curand.h> |
|||
#include <cublas_v2.h> |
|||
|
|||
#ifdef CUDNN |
|||
#pragma comment(lib, "cudnn.lib") |
|||
#endif |
|||
|
|||
#include "cuda.h" |
|||
#include "../base/basicCortex.h" |
|||
#include "../layers/convolutionalLayer.h" |
|||
#include "../cpu/gemm.h" |
|||
|
|||
#define LOG( X ) m_pLog->Log( "CUDA", this, X ) |
|||
#define LOG_ERROR( X ) m_pLog->Log( "CUDA", this, _LOG_ERROR, X ) |
|||
|
|||
|
|||
__device__ float lhtan_activate_kernel(float x){ |
|||
if (x < 0) return .001*x; |
|||
if (x > 1) return .001*(x - 1) + 1; |
|||
return x; |
|||
}; |
|||
|
|||
__device__ float lhtan_gradient_kernel(float x) |
|||
{ |
|||
if (x > 0 && x < 1) return 1; |
|||
return .001; |
|||
} |
|||
|
|||
__device__ float hardtan_activate_kernel(float x) |
|||
{ |
|||
if (x < -1) return -1; |
|||
if (x > 1) return 1; |
|||
return x; |
|||
} |
|||
__device__ float linear_activate_kernel(float x){ return x; } |
|||
__device__ float logistic_activate_kernel(float x){ return 1. / (1. + exp(-x)); } |
|||
__device__ float loggy_activate_kernel(float x){ return 2. / (1. + exp(-x)) - 1; } |
|||
__device__ float relu_activate_kernel(float x){ return x*(x>0); } |
|||
__device__ float elu_activate_kernel(float x){ return (x >= 0)*x + (x < 0)*(exp(x) - 1); } |
|||
__device__ float relie_activate_kernel(float x){ return (x>0) ? x : .01*x; } |
|||
__device__ float ramp_activate_kernel(float x){ return x*(x>0) + .1*x; } |
|||
__device__ float leaky_activate_kernel(float x){ return (x>0) ? x : .1*x; } |
|||
__device__ float tanh_activate_kernel(float x){ return (2 / (1 + exp(-2 * x)) - 1); } |
|||
__device__ float plse_activate_kernel(float x){ |
|||
if (x < -4) return .01 * (x + 4); |
|||
if (x > 4) return .01 * (x - 4) + 1; |
|||
return .125*x + .5; |
|||
} |
|||
__device__ float stair_activate_kernel(float x){ |
|||
int n = floor(x); |
|||
if (n % 2 == 0) return floor(x / 2.); |
|||
else return (x - n) + floor(x / 2.); |
|||
} |
|||
|
|||
|
|||
__device__ float hardtan_gradient_kernel(float x){ |
|||
if (x > -1 && x < 1) return 1; |
|||
return 0; |
|||
} |
|||
__device__ float linear_gradient_kernel(float x){ return 1; } |
|||
__device__ float logistic_gradient_kernel(float x){ return (1 - x)*x; } |
|||
__device__ float loggy_gradient_kernel(float x){ |
|||
float y = (x + 1.) / 2.; |
|||
return 2 * (1 - y)*y; |
|||
} |
|||
__device__ float relu_gradient_kernel(float x){ return (x>0); } |
|||
__device__ float elu_gradient_kernel(float x){ return (x >= 0) + (x < 0)*(x + 1); } |
|||
__device__ float relie_gradient_kernel(float x){ return (x>0) ? 1 : .01; } |
|||
__device__ float ramp_gradient_kernel(float x){ return (x>0) + .1; } |
|||
__device__ float leaky_gradient_kernel(float x){ return (x>0) ? 1 : .1; } |
|||
__device__ float tanh_gradient_kernel(float x){ return 1 - x*x; } |
|||
__device__ float plse_gradient_kernel(float x){ return (x < 0 || x > 1) ? .01 : .125; } |
|||
__device__ float stair_gradient_kernel(float x){ |
|||
if (floor(x) == x) return 0; |
|||
return 1; |
|||
} |
|||
|
|||
__device__ float activate_kernel(float x, ACTIVATION a){ |
|||
switch (a){ |
|||
case LINEAR: |
|||
return linear_activate_kernel(x); |
|||
case LOGISTIC: |
|||
return logistic_activate_kernel(x); |
|||
case LOGGY: |
|||
return loggy_activate_kernel(x); |
|||
case RELU: |
|||
return relu_activate_kernel(x); |
|||
case ELU: |
|||
return elu_activate_kernel(x); |
|||
case RELIE: |
|||
return relie_activate_kernel(x); |
|||
case RAMP: |
|||
return ramp_activate_kernel(x); |
|||
case LEAKY: |
|||
return leaky_activate_kernel(x); |
|||
case TANH: |
|||
return tanh_activate_kernel(x); |
|||
case PLSE: |
|||
return plse_activate_kernel(x); |
|||
case STAIR: |
|||
return stair_activate_kernel(x); |
|||
case HARDTAN: |
|||
return hardtan_activate_kernel(x); |
|||
case LHTAN: |
|||
return lhtan_activate_kernel(x); |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
__device__ float gradient_kernel(float x, ACTIVATION a){ |
|||
switch (a){ |
|||
case LINEAR: |
|||
return linear_gradient_kernel(x); |
|||
case LOGISTIC: |
|||
return logistic_gradient_kernel(x); |
|||
case LOGGY: |
|||
return loggy_gradient_kernel(x); |
|||
case RELU: |
|||
return relu_gradient_kernel(x); |
|||
case ELU: |
|||
return elu_gradient_kernel(x); |
|||
case RELIE: |
|||
return relie_gradient_kernel(x); |
|||
case RAMP: |
|||
return ramp_gradient_kernel(x); |
|||
case LEAKY: |
|||
return leaky_gradient_kernel(x); |
|||
case TANH: |
|||
return tanh_gradient_kernel(x); |
|||
case PLSE: |
|||
return plse_gradient_kernel(x); |
|||
case STAIR: |
|||
return stair_gradient_kernel(x); |
|||
case HARDTAN: |
|||
return hardtan_gradient_kernel(x); |
|||
case LHTAN: |
|||
return lhtan_gradient_kernel(x); |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
///*__global__*/void gpuCUDA::activate_array_kernel(float *x, int n, ACTIVATION a){ |
|||
__global__ void activate_array_kernel(float *x, int n, ACTIVATION a){ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (i < n) x[i] = activate_kernel(x[i], a); |
|||
} |
|||
|
|||
///*__global__*/ void gpuCUDA::gradient_array_kernel(float *x, int n, ACTIVATION a, float *delta){ |
|||
__global__ void gradient_array_kernel(float *x, int n, ACTIVATION a, float *delta){ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (i < n) delta[i] *= gradient_kernel(x[i], a); |
|||
} |
|||
|
|||
bool gpuCUDA::makeActivateArray(float *x, uint32_t n, ACTIVATION a){ |
|||
activate_array_kernel << < gridSize(n), BLOCK >> >(x, n, a); |
|||
return checkError(); |
|||
} |
|||
|
|||
bool gpuCUDA::makeGradientArray(float *x, uint32_t n, ACTIVATION a, float *delta){ |
|||
gradient_array_kernel << < gridSize(n), BLOCK >> >(x, n, a, delta); |
|||
return checkError(); |
|||
} |
|||
|
|||
|
|||
|
|||
__global__ void binarize_kernel(float *x, int n, float *binary){ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (i >= n) return; |
|||
binary[i] = (x[i] >= 0) ? 1 : -1; |
|||
} |
|||
|
|||
bool gpuCUDA::binarize(float *x, uint32_t n, float *binary){ |
|||
binarize_kernel <<< gridSize(n), BLOCK >>>(x, n, binary); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void binarize_input_kernel(float *input, int n, int size, float *binary){ |
|||
int s = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (s >= size) return; |
|||
int i = 0; |
|||
float mean = 0; |
|||
for (i = 0; i < n; ++i){ |
|||
mean += abs(input[i*size + s]); |
|||
} |
|||
mean = mean / n; |
|||
for (i = 0; i < n; ++i){ |
|||
binary[i*size + s] = (input[i*size + s] > 0) ? mean : -mean; |
|||
} |
|||
} |
|||
|
|||
bool gpuCUDA::binarizeInput(float *input, uint32_t n, uint32_t size, float *binary){ |
|||
binarize_input_kernel <<< gridSize(size), BLOCK >>>(input, n, size, binary); |
|||
return checkError(); |
|||
} |
|||
|
|||
|
|||
__global__ void binarize_weights_kernel(float *weights, int n, int size, float *binary){ |
|||
int f = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (f >= n) return; |
|||
int i = 0; |
|||
float mean = 0; |
|||
for (i = 0; i < size; ++i){ |
|||
mean += abs(weights[f*size + i]); |
|||
} |
|||
mean = mean / size; |
|||
for (i = 0; i < size; ++i){ |
|||
binary[f*size + i] = (weights[f*size + i] > 0) ? mean : -mean; |
|||
//binary[f*size + i] = weights[f*size + i]; |
|||
} |
|||
} |
|||
|
|||
bool gpuCUDA::binarizeWeights(float *weights, uint32_t n, uint32_t size, float *binary){ |
|||
binarize_weights_kernel <<< gridSize(n), BLOCK >>>(weights, n, size, binary); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void scale_bias_kernel(float *output, float *biases, int n, int size) |
|||
{ |
|||
int offset = blockIdx.x * blockDim.x + threadIdx.x; |
|||
int filter = blockIdx.y; |
|||
int batch = blockIdx.z; |
|||
|
|||
if (offset < size) output[(batch*n + filter)*size + offset] *= biases[filter]; |
|||
} |
|||
|
|||
bool gpuCUDA::scaleBias(float *output, float *biases, uint32_t batch, uint32_t n, uint32_t size){ |
|||
dim3 dimGrid((size - 1) / BLOCK + 1, n, batch); |
|||
dim3 dimBlock(BLOCK, 1, 1); |
|||
scale_bias_kernel << < dimGrid, dimBlock >> >(output, biases, n, size); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void add_bias_kernel(float *output, float *biases, int n, int size) |
|||
{ |
|||
int offset = blockIdx.x * blockDim.x + threadIdx.x; |
|||
int filter = blockIdx.y; |
|||
int batch = blockIdx.z; |
|||
|
|||
if (offset < size) output[(batch*n + filter)*size + offset] += biases[filter]; |
|||
} |
|||
|
|||
bool gpuCUDA::addBias(float *output, float *biases, uint32_t batch, uint32_t n, uint32_t size){ |
|||
dim3 dimGrid((size - 1) / BLOCK + 1, n, batch); |
|||
dim3 dimBlock(BLOCK, 1, 1); |
|||
add_bias_kernel << <dimGrid, dimBlock >> >(output, biases, n, size); |
|||
return checkError(); |
|||
}; |
|||
|
|||
|
|||
#undef LOG |
|||
#undef LOG_ERROR |
|||
@ -0,0 +1,53 @@ |
|||
#include "cuda_runtime.h" |
|||
#include "curand.h" |
|||
#include "cublas_v2.h" |
|||
#include "cuda.h" |
|||
#include "../layers/avgpoolLayer.h" |
|||
|
|||
__global__ void forward_avgpool_layer_kernel(int n, int w, int h, int c, float *input, float *output) |
|||
{ |
|||
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (id >= n) return; |
|||
|
|||
int k = id % c; |
|||
id /= c; |
|||
int b = id; |
|||
|
|||
int i; |
|||
int out_index = (k + c*b); |
|||
output[out_index] = 0; |
|||
for (i = 0; i < w*h; ++i){ |
|||
int in_index = i + h*w*(k + b*c); |
|||
output[out_index] += input[in_index]; |
|||
} |
|||
output[out_index] /= w*h; |
|||
} |
|||
|
|||
__global__ void backward_avgpool_layer_kernel(int n, int w, int h, int c, float *in_delta, float *out_delta) |
|||
{ |
|||
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (id >= n) return; |
|||
|
|||
int k = id % c; |
|||
id /= c; |
|||
int b = id; |
|||
|
|||
int i; |
|||
int out_index = (k + c*b); |
|||
for (i = 0; i < w*h; ++i){ |
|||
int in_index = i + h*w*(k + b*c); |
|||
in_delta[in_index] += out_delta[out_index] / (w*h); |
|||
} |
|||
} |
|||
|
|||
void gpuCUDA::forward_layer(avgpoolLayer *layer, cortexSTATE state){ |
|||
uint32_t Area = layer->m_nIn_Channels * layer->m_nBatch; |
|||
forward_avgpool_layer_kernel << <gridSize(Area), BLOCK >> >(Area, layer->m_nIn_Width, layer->m_nIn_Height, layer->m_nIn_Channels, state.input, layer->m_pGpuOutput); |
|||
checkError(); |
|||
}; |
|||
|
|||
void gpuCUDA::backward_layer(avgpoolLayer *layer, cortexSTATE state){ |
|||
uint32_t Area = layer->m_nIn_Channels * layer->m_nBatch; |
|||
backward_avgpool_layer_kernel << <gridSize(Area), BLOCK >> >(Area, layer->m_nIn_Width, layer->m_nIn_Height, layer->m_nIn_Channels, state.delta, layer->m_pGpuDelta); |
|||
checkError(); |
|||
}; |
|||
@ -0,0 +1,121 @@ |
|||
#include "cuda_runtime.h" |
|||
#include "curand.h" |
|||
#include "cublas_v2.h" |
|||
#include <assert.h> |
|||
#include "../cpu/blas.h" |
|||
#include "cuda.h" |
|||
#include "../base/basicCortex.h" |
|||
|
|||
|
|||
__global__ void axpy_kernel(int N, float ALPHA, float *X, int OFFX, int INCX, float *Y, int OFFY, int INCY) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) Y[OFFY+i*INCY] += ALPHA*X[OFFX+i*INCX]; |
|||
} |
|||
|
|||
__global__ void pow_kernel(int N, float ALPHA, float *X, int INCX, float *Y, int INCY) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) Y[i*INCY] = pow(X[i*INCX], ALPHA); |
|||
} |
|||
|
|||
__global__ void supp_kernel(int N, float ALPHA, float *X, int INCX) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) { |
|||
if((X[i*INCX] * X[i*INCX]) < (ALPHA * ALPHA)) X[i*INCX] = 0; |
|||
} |
|||
} |
|||
|
|||
__global__ void scal_kernel(int N, float ALPHA, float *X, int INCX) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) X[i*INCX] *= ALPHA; |
|||
} |
|||
|
|||
__global__ void mul_kernel(int N, float *X, int INCX, float *Y, int INCY) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) Y[i*INCY] *= X[i*INCX]; |
|||
} |
|||
|
|||
//extern "C" void axpy_ongpu(int N, float ALPHA, float * X, int INCX, float * Y, int INCY) |
|||
bool gpuCUDA::calcAxpy(uint32_t N, float ALPHA, float * X, uint32_t INCX, float * Y, uint32_t INCY){ |
|||
return calcAxpyOffset(N, ALPHA, X, 0, INCX, Y, 0, INCY); |
|||
}; |
|||
|
|||
//extern "C" void pow_ongpu(int N, float ALPHA, float * X, int INCX, float * Y, int INCY) |
|||
bool gpuCUDA::calcPow(uint32_t N, float ALPHA, float * X, uint32_t INCX, float * Y, uint32_t INCY){ |
|||
pow_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX, Y, INCY); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void axpy_ongpu_offset(int N, float ALPHA, float * X, int OFFX, int INCX, float * Y, int OFFY, int INCY) |
|||
bool gpuCUDA::calcAxpyOffset(uint32_t N, float ALPHA, float * X, uint32_t OFFX, uint32_t INCX, float * Y, uint32_t OFFY, uint32_t INCY){ |
|||
axpy_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, OFFX, INCX, Y, OFFY, INCY); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void mul_ongpu(int N, float * X, int INCX, float * Y, int INCY) |
|||
bool gpuCUDA::calcMul(uint32_t N, float * X, uint32_t INCX, float * Y, uint32_t INCY){ |
|||
mul_kernel<<<gridSize(N), BLOCK>>>(N, X, INCX, Y, INCY); |
|||
return checkError(); |
|||
}; |
|||
|
|||
|
|||
//extern "C" void scal_ongpu(int N, float ALPHA, float * X, int INCX) |
|||
bool gpuCUDA::calcScal(uint32_t N, float ALPHA, float * X, uint32_t INCX){ |
|||
scal_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void supp_ongpu(int N, float ALPHA, float * X, int INCX) |
|||
bool gpuCUDA::calcSupp(uint32_t N, float ALPHA, float * X, uint32_t INCX){ |
|||
supp_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX); |
|||
return checkError(); |
|||
}; |
|||
|
|||
|
|||
__global__ void weighted_sum_kernel(int n, float *a, float *b, float *s, float *c) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < n){ |
|||
c[i] = s[i]*a[i] + (1-s[i])*(b ? b[i] : 0); |
|||
} |
|||
} |
|||
|
|||
//extern "C" void weighted_sum_gpu(float *a, float *b, float *s, int num, float *c) |
|||
bool gpuCUDA::calcWeightedSum(float *a, float *b, float *s, uint32_t num, float *c){ |
|||
weighted_sum_kernel<<<gridSize(num), BLOCK>>>(num, a, b, s, c); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void weighted_delta_kernel(int n, float *a, float *b, float *s, float *da, float *db, float *ds, float *dc) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < n){ |
|||
if(da) da[i] += dc[i] * s[i]; |
|||
db[i] += dc[i] * (1-s[i]); |
|||
ds[i] += dc[i] * a[i] + dc[i] * -b[i]; |
|||
}; |
|||
}; |
|||
|
|||
//extern "C" void weighted_delta_gpu(float *a, float *b, float *s, float *da, float *db, float *ds, int num, float *dc) |
|||
bool gpuCUDA::calcWeightedDelta(float *a, float *b, float *s, float *da, float *db, float *ds, uint32_t num, float *dc){ |
|||
weighted_delta_kernel<<<gridSize(num), BLOCK>>>(num, a, b, s, da, db, ds, dc); |
|||
return checkError();// cudaPeekAtLastError()); |
|||
}; |
|||
|
|||
__global__ void mult_add_into_kernel(int n, float *a, float *b, float *c) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < n){ |
|||
c[i] += a[i]*b[i]; |
|||
}; |
|||
}; |
|||
|
|||
//extern "C" void mult_add_into_gpu(int num, float *a, float *b, float *c) |
|||
bool gpuCUDA::calcMultAddInto(uint32_t num, float *a, float *b, float *c){ |
|||
mult_add_into_kernel<<<gridSize(num), BLOCK>>>( num, a, b, c ); |
|||
return checkError();// cudaPeekAtLastError()); |
|||
}; |
|||
@ -0,0 +1,249 @@ |
|||
#include "cuda_runtime.h" |
|||
#include "curand.h" |
|||
#include "cublas_v2.h" |
|||
|
|||
//extern "C" { |
|||
|
|||
#include "../base/basicUtils.h" |
|||
#include "cuda.h" |
|||
#include "../base/basicImage.h" |
|||
#include "../layers/cropLayer.h" |
|||
//} |
|||
|
|||
__device__ float get_pixel_kernel(float *image, int w, int h, int x, int y, int c) |
|||
{ |
|||
if(x < 0 || x >= w || y < 0 || y >= h) return 0; |
|||
return image[x + w*(y + c*h)]; |
|||
} |
|||
|
|||
__device__ float3 rgb_to_hsv_kernel(float3 rgb) |
|||
{ |
|||
float r = rgb.x; |
|||
float g = rgb.y; |
|||
float b = rgb.z; |
|||
|
|||
float h, s, v; |
|||
float max = (r > g) ? ( (r > b) ? r : b) : ( (g > b) ? g : b); |
|||
float min = (r < g) ? ( (r < b) ? r : b) : ( (g < b) ? g : b); |
|||
float delta = max - min; |
|||
v = max; |
|||
if(max == 0){ |
|||
s = 0; |
|||
h = -1; |
|||
}else{ |
|||
s = delta/max; |
|||
if(r == max){ |
|||
h = (g - b) / delta; |
|||
} else if (g == max) { |
|||
h = 2 + (b - r) / delta; |
|||
} else { |
|||
h = 4 + (r - g) / delta; |
|||
} |
|||
if (h < 0) h += 6; |
|||
} |
|||
return make_float3(h, s, v); |
|||
} |
|||
|
|||
__device__ float3 hsv_to_rgb_kernel(float3 hsv) |
|||
{ |
|||
float h = hsv.x; |
|||
float s = hsv.y; |
|||
float v = hsv.z; |
|||
|
|||
float r, g, b; |
|||
float f, p, q, t; |
|||
|
|||
if (s == 0) { |
|||
r = g = b = v; |
|||
} else { |
|||
int index = (int) floorf(h); |
|||
f = h - index; |
|||
p = v*(1-s); |
|||
q = v*(1-s*f); |
|||
t = v*(1-s*(1-f)); |
|||
if(index == 0){ |
|||
r = v; g = t; b = p; |
|||
} else if(index == 1){ |
|||
r = q; g = v; b = p; |
|||
} else if(index == 2){ |
|||
r = p; g = v; b = t; |
|||
} else if(index == 3){ |
|||
r = p; g = q; b = v; |
|||
} else if(index == 4){ |
|||
r = t; g = p; b = v; |
|||
} else { |
|||
r = v; g = p; b = q; |
|||
} |
|||
} |
|||
r = (r < 0) ? 0 : ((r > 1) ? 1 : r); |
|||
g = (g < 0) ? 0 : ((g > 1) ? 1 : g); |
|||
b = (b < 0) ? 0 : ((b > 1) ? 1 : b); |
|||
return make_float3(r, g, b); |
|||
} |
|||
|
|||
__device__ float bilinear_interpolate_kernel(float *image, int w, int h, float x, float y, int c) |
|||
{ |
|||
int ix = (int) floorf(x); |
|||
int iy = (int) floorf(y); |
|||
|
|||
float dx = x - ix; |
|||
float dy = y - iy; |
|||
|
|||
float val = (1-dy) * (1-dx) * get_pixel_kernel(image, w, h, ix, iy, c) + |
|||
dy * (1-dx) * get_pixel_kernel(image, w, h, ix, iy+1, c) + |
|||
(1-dy) * dx * get_pixel_kernel(image, w, h, ix+1, iy, c) + |
|||
dy * dx * get_pixel_kernel(image, w, h, ix+1, iy+1, c); |
|||
return val; |
|||
} |
|||
|
|||
__global__ void levels_image_kernel(float *image, float *rand, int batch, int w, int h, int train, float saturation, float exposure, float translate, float scale, float shift) |
|||
{ |
|||
int size = batch * w * h; |
|||
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(id >= size) return; |
|||
int x = id % w; |
|||
id /= w; |
|||
int y = id % h; |
|||
id /= h; |
|||
float rshift = rand[0]; |
|||
float gshift = rand[1]; |
|||
float bshift = rand[2]; |
|||
float r0 = rand[8*id + 0]; |
|||
float r1 = rand[8*id + 1]; |
|||
float r2 = rand[8*id + 2]; |
|||
float r3 = rand[8*id + 3]; |
|||
|
|||
saturation = r0*(saturation - 1) + 1; |
|||
saturation = (r1 > .5) ? 1./saturation : saturation; |
|||
exposure = r2*(exposure - 1) + 1; |
|||
exposure = (r3 > .5) ? 1./exposure : exposure; |
|||
|
|||
size_t offset = id * h * w * 3; |
|||
image += offset; |
|||
float r = image[x + w*(y + h*0)]; |
|||
float g = image[x + w*(y + h*1)]; |
|||
float b = image[x + w*(y + h*2)]; |
|||
float3 rgb = make_float3(r,g,b); |
|||
if(train){ |
|||
float3 hsv = rgb_to_hsv_kernel(rgb); |
|||
hsv.y *= saturation; |
|||
hsv.z *= exposure; |
|||
rgb = hsv_to_rgb_kernel(hsv); |
|||
} else { |
|||
shift = 0; |
|||
} |
|||
image[x + w*(y + h*0)] = rgb.x*scale + translate + (rshift - .5)*shift; |
|||
image[x + w*(y + h*1)] = rgb.y*scale + translate + (gshift - .5)*shift; |
|||
image[x + w*(y + h*2)] = rgb.z*scale + translate + (bshift - .5)*shift; |
|||
} |
|||
|
|||
__global__ void forward_crop_layer_kernel(float *input, float *rand, int size, int c, int h, int w, int crop_height, int crop_width, int train, int flip, float angle, float *output) |
|||
{ |
|||
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(id >= size) return; |
|||
|
|||
float cx = w/2.; |
|||
float cy = h/2.; |
|||
|
|||
int count = id; |
|||
int j = id % crop_width; |
|||
id /= crop_width; |
|||
int i = id % crop_height; |
|||
id /= crop_height; |
|||
int k = id % c; |
|||
id /= c; |
|||
int b = id; |
|||
|
|||
float r4 = rand[8*b + 4]; |
|||
float r5 = rand[8*b + 5]; |
|||
float r6 = rand[8*b + 6]; |
|||
float r7 = rand[8*b + 7]; |
|||
|
|||
float dw = (w - crop_width)*r4; |
|||
float dh = (h - crop_height)*r5; |
|||
flip = (flip && (r6 > .5)); |
|||
angle = 2*angle*r7 - angle; |
|||
if(!train){ |
|||
dw = (w - crop_width)/2.; |
|||
dh = (h - crop_height)/2.; |
|||
flip = 0; |
|||
angle = 0; |
|||
} |
|||
|
|||
input += w*h*c*b; |
|||
|
|||
float x = (flip) ? w - dw - j - 1 : j + dw; |
|||
float y = i + dh; |
|||
|
|||
float rx = cos(angle)*(x-cx) - sin(angle)*(y-cy) + cx; |
|||
float ry = sin(angle)*(x-cx) + cos(angle)*(y-cy) + cy; |
|||
|
|||
output[count] = bilinear_interpolate_kernel(input, w, h, rx, ry, k); |
|||
} |
|||
|
|||
void gpuCUDA::forward_layer( cropLayer *layer, cortexSTATE state ){ |
|||
makeRandom( layer->m_pGpuRand, layer->m_nBatch << 3 );// *8); |
|||
|
|||
float rad_angle = layer->m_fAngle * 3.14159265f/180.f; |
|||
|
|||
float scale = 2.f; |
|||
float translate = -1.f; |
|||
if( layer->m_bNoAdjust ){ |
|||
scale = 1.f; |
|||
translate = 0.f; |
|||
}; |
|||
int size = layer->m_nBatch * layer->m_nIn_Width * layer->m_nIn_Height; |
|||
|
|||
levels_image_kernel<<<gridSize(size), BLOCK>>>( |
|||
state.input, |
|||
layer->m_pGpuRand, |
|||
layer->m_nBatch, |
|||
layer->m_nIn_Width, |
|||
layer->m_nIn_Height, |
|||
state.train, |
|||
layer->m_fSaturation, |
|||
layer->m_fExposure, |
|||
translate, |
|||
scale, |
|||
layer->m_fShift |
|||
); |
|||
checkError();// cudaPeekAtLastError()); |
|||
|
|||
size = layer->m_nBatch * layer->m_nIn_Channels * layer->m_nOut_Width * layer->m_nOut_Height; |
|||
|
|||
forward_crop_layer_kernel<<<gridSize(size), BLOCK>>>( |
|||
state.input, |
|||
layer->m_pGpuRand, |
|||
size, |
|||
layer->m_nIn_Channels, |
|||
layer->m_nIn_Height, |
|||
layer->m_nIn_Width, |
|||
layer->m_nOut_Height, |
|||
layer->m_nOut_Width, |
|||
state.train, |
|||
layer->m_bFlipped, |
|||
rad_angle, |
|||
layer->m_pGpuOutput |
|||
); |
|||
checkError();// cudaPeekAtLastError()); |
|||
|
|||
/* |
|||
cuda_pull_array(layer.output_gpu, layer.output, size); |
|||
image im = float_to_image(layer.crop_width, layer.crop_height, layer.c, layer.output + 0*(size/layer.batch)); |
|||
image im2 = float_to_image(layer.crop_width, layer.crop_height, layer.c, layer.output + 1*(size/layer.batch)); |
|||
image im3 = float_to_image(layer.crop_width, layer.crop_height, layer.c, layer.output + 2*(size/layer.batch)); |
|||
|
|||
translate_image(im, -translate); |
|||
scale_image(im, 1/scale); |
|||
translate_image(im2, -translate); |
|||
scale_image(im2, 1/scale); |
|||
translate_image(im3, -translate); |
|||
scale_image(im3, 1/scale); |
|||
|
|||
show_image(im, "cropped"); |
|||
show_image(im2, "cropped2"); |
|||
show_image(im3, "cropped3"); |
|||
cvWaitKey(0); |
|||
*/ |
|||
} |
|||
|
|||
@ -0,0 +1,47 @@ |
|||
#include <cuda_runtime.h> |
|||
#include <curand.h> |
|||
#include <cublas_v2.h> |
|||
|
|||
#include "cuda.h" |
|||
|
|||
#include "../layers/dropoutLayer.h" |
|||
|
|||
__global__ void yoloswag420blazeit360noscope(float *input, int size, float *rand, float prob, float scale) |
|||
{ |
|||
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(id < size) input[id] = (rand[id] < prob) ? 0 : input[id]*scale; |
|||
} |
|||
|
|||
void gpuCUDA::forward_layer(dropoutLayer *layer, cortexSTATE state){ |
|||
if (!state.train) return; |
|||
int size = layer->m_nInput * layer->m_nBatch; |
|||
makeRandom(layer->m_pGpuRand, size); |
|||
/* |
|||
int i; |
|||
for(i = 0; i < size; ++i){ |
|||
layer.rand[i] = rand_uniform(); |
|||
} |
|||
cuda_push_array(layer.rand_gpu, layer.rand, size); |
|||
*/ |
|||
yoloswag420blazeit360noscope << <gridSize(size), BLOCK >> >( |
|||
state.input, |
|||
size, |
|||
layer->m_pGpuRand, |
|||
layer->m_fProbability, |
|||
layer->m_fScale |
|||
); |
|||
checkError();// cudaPeekAtLastError()); |
|||
} |
|||
|
|||
void gpuCUDA::backward_layer(dropoutLayer *layer, cortexSTATE state){ |
|||
if(!state.delta) return; |
|||
int size = layer->m_nInput*layer->m_nBatch; |
|||
yoloswag420blazeit360noscope << <gridSize(size), BLOCK >> >( |
|||
state.delta, |
|||
size, |
|||
layer->m_pGpuRand, |
|||
layer->m_fProbability, |
|||
layer->m_fScale |
|||
); |
|||
checkError();// cudaPeekAtLastError()); |
|||
} |
|||
@ -0,0 +1,614 @@ |
|||
#include "cuda_runtime.h" |
|||
#include "curand.h" |
|||
#include "cublas_v2.h" |
|||
#include <assert.h> |
|||
#include "../cpu/blas.h" |
|||
#include "cuda.h" |
|||
#include "../base/basicCortex.h" |
|||
|
|||
__global__ void const_kernel(int N, float ALPHA, float *X, int INCX) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) X[i*INCX] = ALPHA; |
|||
} |
|||
|
|||
__global__ void constrain_kernel(int N, float ALPHA, float *X, int INCX) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) X[i*INCX] = fminf(ALPHA, fmaxf(-ALPHA, X[i*INCX])); |
|||
} |
|||
|
|||
__global__ void fill_kernel(int N, float ALPHA, float *X, int INCX) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) X[i*INCX] = ALPHA; |
|||
} |
|||
|
|||
__global__ void mask_kernel(int n, float *x, float mask_num, float *mask) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < n && mask[i] == mask_num) x[i] = mask_num; |
|||
} |
|||
|
|||
__global__ void copy_kernel(int N, float *X, int OFFX, int INCX, float *Y, int OFFY, int INCY) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < N) Y[i*INCY + OFFY] = X[i*INCX + OFFX]; |
|||
} |
|||
|
|||
__global__ void backward_scale_kernel(float *x_norm, float *delta, int batch, int n, int size, float *scale_updates) |
|||
{ |
|||
__shared__ float part[BLOCK]; |
|||
int i,b; |
|||
int filter = blockIdx.x; |
|||
int p = threadIdx.x; |
|||
float sum = 0; |
|||
for(b = 0; b < batch; ++b){ |
|||
for(i = 0; i < size; i += BLOCK){ |
|||
int index = p + i + size*(filter + n*b); |
|||
sum += (p+i < size) ? delta[index]*x_norm[index] : 0; |
|||
} |
|||
} |
|||
part[p] = sum; |
|||
__syncthreads(); |
|||
if (p == 0) { |
|||
for(i = 0; i < BLOCK; ++i) scale_updates[filter] += part[i]; |
|||
} |
|||
} |
|||
|
|||
bool gpuCUDA::makeBackwardScale(float *x_norm, float *delta, uint32_t batch, uint32_t n, uint32_t size, float *scale_updates){ |
|||
backward_scale_kernel<<<n, BLOCK>>>(x_norm, delta, batch, n, size, scale_updates); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void backward_bias_kernel(float *bias_updates, float *delta, int batch, int n, int size) |
|||
{ |
|||
__shared__ float part[BLOCK]; |
|||
int i,b; |
|||
int filter = blockIdx.x; |
|||
int p = threadIdx.x; |
|||
float sum = 0; |
|||
for(b = 0; b < batch; ++b){ |
|||
for(i = 0; i < size; i += BLOCK){ |
|||
int index = p + i + size*(filter + n*b); |
|||
sum += (p+i < size) ? delta[index] : 0; |
|||
} |
|||
} |
|||
part[p] = sum; |
|||
__syncthreads(); |
|||
if (p == 0) { |
|||
for(i = 0; i < BLOCK; ++i) bias_updates[filter] += part[i]; |
|||
} |
|||
} |
|||
|
|||
__global__ void dot_kernel(float *output, float scale, int batch, int n, int size, float *delta) |
|||
{ |
|||
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
int f1 = index / n; |
|||
int f2 = index % n; |
|||
if (f2 <= f1) return; |
|||
|
|||
float sum = 0; |
|||
float norm1 = 0; |
|||
float norm2 = 0; |
|||
int b, i; |
|||
for(b = 0; b < batch; ++b){ |
|||
for(i = 0; i < size; ++i){ |
|||
int i1 = b * size * n + f1 * size + i; |
|||
int i2 = b * size * n + f2 * size + i; |
|||
sum += output[i1] * output[i2]; |
|||
norm1 += output[i1] * output[i1]; |
|||
norm2 += output[i2] * output[i2]; |
|||
} |
|||
} |
|||
norm1 = sqrt(norm1); |
|||
norm2 = sqrt(norm2); |
|||
float norm = norm1 * norm2; |
|||
sum = sum / norm; |
|||
for(b = 0; b < batch; ++b){ |
|||
for(i = 0; i < size; ++i){ |
|||
int i1 = b * size * n + f1 * size + i; |
|||
int i2 = b * size * n + f2 * size + i; |
|||
delta[i1] += - scale * sum * output[i2] / norm; |
|||
delta[i2] += - scale * sum * output[i1] / norm; |
|||
} |
|||
} |
|||
}; |
|||
|
|||
bool gpuCUDA::makeDotError(cortexLayer *l){ |
|||
dot_kernel <<< gridSize(l->m_nN*l->m_nN), BLOCK >>> (l->m_pGpuOutput, l->m_fDOT, l->m_nBatch, l->m_nN, l->m_nOut_Width * l->m_nOut_Height, l->m_pGpuDelta); |
|||
return checkError(); |
|||
}; |
|||
|
|||
bool gpuCUDA::makeBackwardBias(float *bias_updates, float *delta, uint32_t batch, uint32_t n, uint32_t size){ |
|||
backward_bias_kernel<<<n, BLOCK>>>(bias_updates, delta, batch, n, size); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void adam_kernel(int N, float *x, float *m, float *v, float B1, float B2, float rate, float eps, int t) |
|||
{ |
|||
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (index >= N) return; |
|||
|
|||
x[index] = x[index] - (rate * sqrt(1.-pow(B2, t)) / (1.-pow(B1, t)) * m[index] / (sqrt(v[index]) + eps)); |
|||
//if(index == 0) printf("%f %f %f %f\n", m[index], v[index], (rate * sqrt(1.-pow(B2, t)) / (1.-pow(B1, t)) * m[index] / (sqrt(v[index]) + eps))); |
|||
} |
|||
|
|||
//extern "C" void adam_gpu(int n, float *x, float *m, float *v, float B1, float B2, float rate, float eps, int t) |
|||
bool gpuCUDA::makeAdam(uint32_t n, float *x, float *m, float *v, float B1, float B2, float rate, float eps, uint32_t t){ |
|||
adam_kernel<<<gridSize(n), BLOCK>>>(n, x, m, v, B1, B2, rate, eps, t); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void normalize_kernel(int N, float *x, float *mean, float *variance, int batch, int filters, int spatial) |
|||
{ |
|||
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (index >= N) return; |
|||
int f = (index/spatial)%filters; |
|||
|
|||
x[index] = (x[index] - mean[f])/(sqrt(variance[f]) + .000001f); |
|||
} |
|||
|
|||
__global__ void normalize_delta_kernel(int N, float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, int batch, int filters, int spatial, float *delta) |
|||
{ |
|||
int index = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (index >= N) return; |
|||
int f = (index/spatial)%filters; |
|||
|
|||
delta[index] = delta[index] * 1./(sqrt(variance[f]) + .000001f) + variance_delta[f] * 2. * (x[index] - mean[f]) / (spatial * batch) + mean_delta[f]/(spatial*batch); |
|||
} |
|||
|
|||
//extern "C" void normalize_delta_gpu(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, int batch, int filters, int spatial, float *delta) |
|||
bool gpuCUDA::makeNormalizedDelta(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, uint32_t batch, uint32_t filters, uint32_t spatial, float *delta){ |
|||
unsigned int N = batch*filters*spatial; |
|||
normalize_delta_kernel<<<gridSize(N), BLOCK>>>(N, x, mean, variance, mean_delta, variance_delta, batch, filters, spatial, delta); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void variance_delta_kernel(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (i >= filters) return; |
|||
int j,k; |
|||
variance_delta[i] = 0; |
|||
for(j = 0; j < batch; ++j){ |
|||
for(k = 0; k < spatial; ++k){ |
|||
int index = j*filters*spatial + i*spatial + k; |
|||
variance_delta[i] += delta[index]*(x[index] - mean[i]); |
|||
} |
|||
} |
|||
variance_delta[i] *= -.5 * pow(variance[i] + .000001f, (float)(-3./2.)); |
|||
} |
|||
|
|||
__global__ void accumulate_kernel(float *x, int n, int groups, float *sum) |
|||
{ |
|||
int k; |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (i >= groups) return; |
|||
sum[i] = 0; |
|||
for(k = 0; k < n; ++k){ |
|||
sum[i] += x[k*groups + i]; |
|||
} |
|||
} |
|||
|
|||
__global__ void fast_mean_delta_kernel(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta) |
|||
{ |
|||
const int threads = BLOCK; |
|||
__shared__ float local[threads]; |
|||
|
|||
int id = threadIdx.x; |
|||
local[id] = 0; |
|||
|
|||
int filter = blockIdx.x; |
|||
|
|||
int i, j; |
|||
for(j = 0; j < batch; ++j){ |
|||
for(i = 0; i < spatial; i += threads){ |
|||
int index = j*spatial*filters + filter*spatial + i + id; |
|||
local[id] += (i+id < spatial) ? delta[index] : 0; |
|||
} |
|||
} |
|||
__syncthreads(); |
|||
|
|||
if(id == 0){ |
|||
mean_delta[filter] = 0; |
|||
for(i = 0; i < threads; ++i){ |
|||
mean_delta[filter] += local[i]; |
|||
} |
|||
mean_delta[filter] *= (-1./sqrt(variance[filter] + .000001f)); |
|||
} |
|||
} |
|||
|
|||
__global__ void fast_variance_delta_kernel(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta) |
|||
{ |
|||
const int threads = BLOCK; |
|||
__shared__ float local[threads]; |
|||
|
|||
int id = threadIdx.x; |
|||
local[id] = 0; |
|||
|
|||
int filter = blockIdx.x; |
|||
|
|||
int i, j; |
|||
for(j = 0; j < batch; ++j){ |
|||
for(i = 0; i < spatial; i += threads){ |
|||
int index = j*spatial*filters + filter*spatial + i + id; |
|||
|
|||
local[id] += (i+id < spatial) ? delta[index]*(x[index] - mean[filter]) : 0; |
|||
} |
|||
} |
|||
__syncthreads(); |
|||
|
|||
if(id == 0){ |
|||
variance_delta[filter] = 0; |
|||
for(i = 0; i < threads; ++i){ |
|||
variance_delta[filter] += local[i]; |
|||
} |
|||
variance_delta[filter] *= -.5 * pow(variance[filter] + .000001f, (float)(-3./2.)); |
|||
} |
|||
} |
|||
|
|||
|
|||
__global__ void mean_delta_kernel(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (i >= filters) return; |
|||
int j,k; |
|||
mean_delta[i] = 0; |
|||
for (j = 0; j < batch; ++j) { |
|||
for (k = 0; k < spatial; ++k) { |
|||
int index = j*filters*spatial + i*spatial + k; |
|||
mean_delta[i] += delta[index]; |
|||
} |
|||
} |
|||
mean_delta[i] *= (-1./sqrt(variance[i] + .000001f)); |
|||
} |
|||
|
|||
//extern "C" void mean_delta_gpu(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta) |
|||
bool gpuCUDA::makeMeanDelta(float *delta, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean_delta){ |
|||
mean_delta_kernel<<<gridSize(filters), BLOCK>>>(delta, variance, batch, filters, spatial, mean_delta); |
|||
return checkError(); |
|||
} |
|||
|
|||
//extern "C" void fast_mean_delta_gpu(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta) |
|||
bool gpuCUDA::makeFastMeanDelta(float *delta, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean_delta){ |
|||
fast_mean_delta_kernel<<<filters, BLOCK>>>(delta, variance, batch, filters, spatial, mean_delta); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void fast_variance_delta_gpu(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta) |
|||
bool gpuCUDA::makeFastVarianceDelta(float *x, float *delta, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance_delta){ |
|||
fast_variance_delta_kernel<<<filters, BLOCK>>>(x, delta, mean, variance, batch, filters, spatial, variance_delta); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void mean_kernel(float *x, int batch, int filters, int spatial, float *mean) |
|||
{ |
|||
float scale = 1./(batch * spatial); |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (i >= filters) return; |
|||
int j,k; |
|||
mean[i] = 0; |
|||
for(j = 0; j < batch; ++j){ |
|||
for(k = 0; k < spatial; ++k){ |
|||
int index = j*filters*spatial + i*spatial + k; |
|||
mean[i] += x[index]; |
|||
} |
|||
} |
|||
mean[i] *= scale; |
|||
} |
|||
|
|||
__global__ void variance_kernel(float *x, float *mean, int batch, int filters, int spatial, float *variance) |
|||
{ |
|||
float scale = 1./(batch * spatial - 1); |
|||
int j,k; |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (i >= filters) return; |
|||
variance[i] = 0; |
|||
for(j = 0; j < batch; ++j){ |
|||
for(k = 0; k < spatial; ++k){ |
|||
int index = j*filters*spatial + i*spatial + k; |
|||
variance[i] += pow((x[index] - mean[i]), 2); |
|||
} |
|||
} |
|||
variance[i] *= scale; |
|||
} |
|||
|
|||
__global__ void reorg_kernel(int N, float *x, int w, int h, int c, int batch, int stride, bool forward, float *out) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i >= N) return; |
|||
int in_index = i; |
|||
int in_w = i%w; |
|||
i = i/w; |
|||
int in_h = i%h; |
|||
i = i/h; |
|||
int in_c = i%c; |
|||
i = i/c; |
|||
int b = i%batch; |
|||
|
|||
int out_c = c/(stride*stride); |
|||
|
|||
int c2 = in_c % out_c; |
|||
int offset = in_c / out_c; |
|||
int w2 = in_w*stride + offset % stride; |
|||
int h2 = in_h*stride + offset / stride; |
|||
//printf("%d\n", offset); |
|||
int out_index = w2 + w*stride*(h2 + h*stride*(c2 + out_c*b)); |
|||
|
|||
// printf("%d %d %d\n", w2, h2, c2); |
|||
//printf("%d %d\n", in_index, out_index); |
|||
//if(out_index >= N || out_index < 0) printf("bad bad bad \n"); |
|||
|
|||
if(forward) out[out_index] = x[in_index]; |
|||
else out[in_index] = x[out_index]; |
|||
//if(forward) out[1] = x[1]; |
|||
//else out[0] = x[0]; |
|||
} |
|||
|
|||
//extern "C" void normalize_gpu(float *x, float *mean, float *variance, int batch, int filters, int spatial) |
|||
bool gpuCUDA::makeNormalized(float *x, float *mean, float *variance, uint32_t batch, uint32_t filters, uint32_t spatial){ |
|||
unsigned int N = batch*filters*spatial; |
|||
normalize_kernel<<<gridSize(N), BLOCK>>>(N, x, mean, variance, batch, filters, spatial); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void fast_mean_kernel(float *x, int batch, int filters, int spatial, float *mean) |
|||
{ |
|||
const int threads = BLOCK; |
|||
__shared__ float local[threads]; |
|||
|
|||
int id = threadIdx.x; |
|||
local[id] = 0; |
|||
|
|||
int filter = blockIdx.x; |
|||
|
|||
int i, j; |
|||
for(j = 0; j < batch; ++j){ |
|||
for(i = 0; i < spatial; i += threads){ |
|||
int index = j*spatial*filters + filter*spatial + i + id; |
|||
local[id] += (i+id < spatial) ? x[index] : 0; |
|||
} |
|||
} |
|||
__syncthreads(); |
|||
|
|||
if(id == 0){ |
|||
mean[filter] = 0; |
|||
for(i = 0; i < threads; ++i){ |
|||
mean[filter] += local[i]; |
|||
} |
|||
mean[filter] /= spatial * batch; |
|||
} |
|||
} |
|||
|
|||
__global__ void fast_variance_kernel(float *x, float *mean, int batch, int filters, int spatial, float *variance) |
|||
{ |
|||
const int threads = BLOCK; |
|||
__shared__ float local[threads]; |
|||
|
|||
int id = threadIdx.x; |
|||
local[id] = 0; |
|||
|
|||
int filter = blockIdx.x; |
|||
|
|||
int i, j; |
|||
for(j = 0; j < batch; ++j){ |
|||
for(i = 0; i < spatial; i += threads){ |
|||
int index = j*spatial*filters + filter*spatial + i + id; |
|||
|
|||
local[id] += (i+id < spatial) ? pow((x[index] - mean[filter]), 2) : 0; |
|||
} |
|||
} |
|||
__syncthreads(); |
|||
|
|||
if(id == 0){ |
|||
variance[filter] = 0; |
|||
for(i = 0; i < threads; ++i){ |
|||
variance[filter] += local[i]; |
|||
} |
|||
variance[filter] /= (spatial * batch - 1); |
|||
} |
|||
} |
|||
|
|||
//extern "C" void fast_mean_gpu(float *x, int batch, int filters, int spatial, float *mean) |
|||
bool gpuCUDA::makeFastMean(float *x, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean){ |
|||
fast_mean_kernel<<<filters, BLOCK>>>(x, batch, filters, spatial, mean); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void fast_variance_gpu(float *x, float *mean, int batch, int filters, int spatial, float *variance) |
|||
bool gpuCUDA::makeFastVariance(float *x, float *mean, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance){ |
|||
fast_variance_kernel<<<filters, BLOCK>>>(x, mean, batch, filters, spatial, variance); |
|||
return checkError(); |
|||
}; |
|||
|
|||
|
|||
//extern "C" void mean_gpu(float *x, int batch, int filters, int spatial, float *mean) |
|||
bool gpuCUDA::makeMean(float *x, uint32_t batch, uint32_t filters, uint32_t spatial, float *mean){ |
|||
mean_kernel<<<gridSize(filters), BLOCK>>>(x, batch, filters, spatial, mean); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void variance_gpu(float *x, float *mean, int batch, int filters, int spatial, float *variance) |
|||
bool gpuCUDA::makeVariance(float *x, float *mean, uint32_t batch, uint32_t filters, uint32_t spatial, float *variance){ |
|||
variance_kernel<<<gridSize(filters), BLOCK>>>(x, mean, batch, filters, spatial, variance); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void copy_ongpu(int N, float * X, int INCX, float * Y, int INCY) |
|||
bool gpuCUDA::makeCopy(size_t N, float * X, size_t INCX, float * Y, size_t INCY){ |
|||
return makeCopyOffset(N, X, 0, INCX, Y, 0, INCY); |
|||
}; |
|||
|
|||
//extern "C" void copy_ongpu_offset(int N, float * X, int OFFX, int INCX, float * Y, int OFFY, int INCY) |
|||
bool gpuCUDA::makeCopyOffset(uint32_t N, float * X, uint32_t OFFX, uint32_t INCX, float * Y, uint32_t OFFY, uint32_t INCY){ |
|||
copy_kernel<<<gridSize(N), BLOCK>>>(N, X, OFFX, INCX, Y, OFFY, INCY); |
|||
return checkError();//cudaPeekAtLastError()); |
|||
}; |
|||
|
|||
__global__ void flatten_kernel(int N, float *x, int spatial, int layers, int batch, int forward, float *out) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i >= N) return; |
|||
int in_s = i%spatial; |
|||
i = i/spatial; |
|||
int in_c = i%layers; |
|||
i = i/layers; |
|||
int b = i; |
|||
|
|||
int i1 = b*layers*spatial + in_c*spatial + in_s; |
|||
int i2 = b*layers*spatial + in_s*layers + in_c; |
|||
|
|||
if (forward) out[i2] = x[i1]; |
|||
else out[i1] = x[i2]; |
|||
} |
|||
|
|||
//extern "C" void flatten_ongpu(float *x, int spatial, int layers, int batch, int forward, float *out) |
|||
bool gpuCUDA::makeFlatten(float *x, uint32_t spatial, uint32_t layers, uint32_t batch, uint32_t forward, float *out){ |
|||
int size = spatial*batch*layers; |
|||
flatten_kernel<<<gridSize(size), BLOCK>>>(size, x, spatial, layers, batch, forward, out); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void reorg_ongpu(float *x, int w, int h, int c, int batch, int stride, int forward, float *out) |
|||
bool gpuCUDA::makeReorg(float *x, uint32_t w, uint32_t h, uint32_t c, uint32_t batch, uint32_t stride, bool forward, float *out){ |
|||
int size = w*h*c*batch; |
|||
reorg_kernel<<<gridSize(size), BLOCK>>>(size, x, w, h, c, batch, stride, forward, out); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void mask_ongpu(int N, float * X, float mask_num, float * mask) |
|||
bool gpuCUDA::makeMask(uint32_t N, float * X, float mask_num, float * mask){ |
|||
mask_kernel<<<gridSize(N), BLOCK>>>(N, X, mask_num, mask); |
|||
return checkError(); |
|||
}; |
|||
|
|||
bool gpuCUDA::makeConst(uint32_t N, float ALPHA, float * X, uint32_t INCX){ |
|||
const_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void constrain_ongpu(int N, float ALPHA, float * X, int INCX) |
|||
bool gpuCUDA::makeConstrain(uint32_t N, float ALPHA, float * X, uint32_t INCX){ |
|||
constrain_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX); |
|||
return checkError(); |
|||
}; |
|||
|
|||
//extern "C" void fill_ongpu(int N, float ALPHA, float * X, int INCX) |
|||
bool gpuCUDA::makeFill(uint32_t N, float ALPHA, float * X, uint32_t INCX){ |
|||
fill_kernel<<<gridSize(N), BLOCK>>>(N, ALPHA, X, INCX); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void shortcut_kernel(int size, int minw, int minh, int minc, int stride, int sample, int batch, int w1, int h1, int c1, float *add, int w2, int h2, int c2, float *out) |
|||
{ |
|||
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if (id >= size) return; |
|||
int i = id % minw; |
|||
id /= minw; |
|||
int j = id % minh; |
|||
id /= minh; |
|||
int k = id % minc; |
|||
id /= minc; |
|||
int b = id % batch; |
|||
|
|||
int out_index = i*sample + w2*(j*sample + h2*(k + c2*b)); |
|||
int add_index = i*stride + w1*(j*stride + h1*(k + c1*b)); |
|||
out[out_index] += add[add_index]; |
|||
} |
|||
|
|||
//extern "C" void shortcut_gpu(int batch, int w1, int h1, int c1, float *add, int w2, int h2, int c2, float *out) |
|||
bool gpuCUDA::makeShortcut(uint32_t batch, uint32_t w1, uint32_t h1, uint32_t c1, float *add, uint32_t w2, uint32_t h2, uint32_t c2, float *out){ |
|||
int minw = (w1 < w2) ? w1 : w2; |
|||
int minh = (h1 < h2) ? h1 : h2; |
|||
int minc = (c1 < c2) ? c1 : c2; |
|||
|
|||
int stride = w1/w2; |
|||
int sample = w2/w1; |
|||
assert(stride == h1/h2); |
|||
assert(sample == h2/h1); |
|||
if(stride < 1) stride = 1; |
|||
if(sample < 1) sample = 1; |
|||
|
|||
int size = batch * minw * minh * minc; |
|||
shortcut_kernel<<<gridSize(size), BLOCK>>>(size, minw, minh, minc, stride, sample, batch, w1, h1, c1, add, w2, h2, c2, out); |
|||
return checkError(); |
|||
}; |
|||
|
|||
__global__ void smooth_l1_kernel(int n, float *pred, float *truth, float *delta, float *error) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < n){ |
|||
float diff = truth[i] - pred[i]; |
|||
float abs_val = abs(diff); |
|||
if(abs_val < 1) { |
|||
error[i] = diff * diff; |
|||
delta[i] = diff; |
|||
} |
|||
else { |
|||
error[i] = 2*abs_val - 1; |
|||
delta[i] = (diff < 0) ? -1 : 1; |
|||
} |
|||
} |
|||
} |
|||
|
|||
//extern "C" void smooth_l1_gpu(int n, float *pred, float *truth, float *delta, float *error) |
|||
bool gpuCUDA::makeSmoothL1(uint32_t n, float *pred, float *truth, float *delta, float *error){ |
|||
smooth_l1_kernel<<<gridSize(n), BLOCK>>>( n, pred, truth, delta, error ); |
|||
return checkError(); |
|||
} |
|||
|
|||
__global__ void l2_kernel(int n, float *pred, float *truth, float *delta, float *error) |
|||
{ |
|||
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(i < n){ |
|||
float diff = truth[i] - pred[i]; |
|||
error[i] = diff * diff; //I know this is technically wrong, deal with it. |
|||
delta[i] = diff; |
|||
} |
|||
} |
|||
|
|||
//extern "C" void l2_gpu(int n, float *pred, float *truth, float *delta, float *error) |
|||
bool gpuCUDA::makeL2(uint32_t n, float *pred, float *truth, float *delta, float *error){ |
|||
l2_kernel<<<gridSize(n), BLOCK>>>( n, pred, truth, delta, error ); |
|||
return checkError(); |
|||
}; |
|||
|
|||
|
|||
__device__ void softmax_device(int n, float *input, float temp, float *output) |
|||
{ |
|||
int i; |
|||
float sum = 0; |
|||
float largest = -INFINITY; |
|||
for(i = 0; i < n; ++i){ |
|||
int val = input[i]; |
|||
largest = (val>largest) ? val : largest; |
|||
} |
|||
for(i = 0; i < n; ++i){ |
|||
float e = exp(input[i]/temp - largest/temp); |
|||
sum += e; |
|||
output[i] = e; |
|||
} |
|||
for(i = 0; i < n; ++i){ |
|||
output[i] /= sum; |
|||
} |
|||
} |
|||
|
|||
__global__ void softmax_kernel(int n, int offset, int batch, float *input, float temp, float *output) |
|||
{ |
|||
int b = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(b >= batch) return; |
|||
softmax_device(n, input + b*offset, temp, output + b*offset); |
|||
} |
|||
|
|||
//extern "C" void softmax_gpu(float *input, int n, int offset, int groups, float temp, float *output) |
|||
bool gpuCUDA::makeSoftMax(float *input, uint32_t n, uint32_t offset, uint32_t groups, float temp, float *output){ |
|||
int inputs = n; |
|||
int batch = groups; |
|||
softmax_kernel<<<gridSize(batch), BLOCK>>>( inputs, offset, batch, input, temp, output ); |
|||
return checkError(); |
|||
}; |
|||
|
|||
|
|||
|
|||
|
|||
@ -0,0 +1,121 @@ |
|||
#include <cuda_runtime.h> |
|||
#include <curand.h> |
|||
#include <cublas_v2.h> |
|||
|
|||
#include "cuda.h" |
|||
#include "../base/basicLayer.h" |
|||
#include "../layers/maxpoolLayer.h" |
|||
|
|||
__global__ void forward_maxpool_layer_kernel(int n, int in_h, int in_w, int in_c, int stride, int size, int pad, float *input, float *output, uint32_t *indexes) |
|||
{ |
|||
int h = (in_h + 2*pad)/stride; |
|||
int w = (in_w + 2*pad)/stride; |
|||
int c = in_c; |
|||
|
|||
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(id >= n) return; |
|||
|
|||
int j = id % w; |
|||
id /= w; |
|||
int i = id % h; |
|||
id /= h; |
|||
int k = id % c; |
|||
id /= c; |
|||
int b = id; |
|||
|
|||
int w_offset = -pad; |
|||
int h_offset = -pad; |
|||
|
|||
int out_index = j + w*(i + h*(k + c*b)); |
|||
float max = -INFINITY; |
|||
int max_i = -1; |
|||
int l, m; |
|||
for(l = 0; l < size; ++l){ |
|||
for(m = 0; m < size; ++m){ |
|||
int cur_h = h_offset + i*stride + l; |
|||
int cur_w = w_offset + j*stride + m; |
|||
int index = cur_w + in_w*(cur_h + in_h*(k + b*in_c)); |
|||
int valid = (cur_h >= 0 && cur_h < in_h && |
|||
cur_w >= 0 && cur_w < in_w); |
|||
float val = (valid != 0) ? input[index] : -INFINITY; |
|||
max_i = (val > max) ? index : max_i; |
|||
max = (val > max) ? val : max; |
|||
} |
|||
} |
|||
output[out_index] = max; |
|||
indexes[out_index] = max_i; |
|||
} |
|||
|
|||
__global__ void backward_maxpool_layer_kernel(int n, int in_h, int in_w, int in_c, int stride, int size, int pad, float *delta, float *prev_delta, uint32_t *indexes) |
|||
{ |
|||
int h = (in_h + 2*pad)/stride; |
|||
int w = (in_w + 2*pad)/stride; |
|||
int c = in_c; |
|||
int area = (size-1)/stride; |
|||
|
|||
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; |
|||
if(id >= n) return; |
|||
|
|||
int index = id; |
|||
int j = id % in_w; |
|||
id /= in_w; |
|||
int i = id % in_h; |
|||
id /= in_h; |
|||
int k = id % in_c; |
|||
id /= in_c; |
|||
int b = id; |
|||
|
|||
int w_offset = -pad; |
|||
int h_offset = -pad; |
|||
|
|||
float d = 0; |
|||
int l, m; |
|||
for(l = -area; l < area+1; ++l){ |
|||
for(m = -area; m < area+1; ++m){ |
|||
int out_w = (j-w_offset)/stride + m; |
|||
int out_h = (i-h_offset)/stride + l; |
|||
int out_index = out_w + w*(out_h + h*(k + c*b)); |
|||
int valid = (out_w >= 0 && out_w < w && |
|||
out_h >= 0 && out_h < h); |
|||
d += (valid && indexes[out_index] == index) ? delta[out_index] : 0; |
|||
} |
|||
} |
|||
prev_delta[index] += d; |
|||
} |
|||
|
|||
//extern "C" void forward_maxpool_layer_gpu(maxpool_layer layer, network_state state) |
|||
void gpuCUDA::forward_layer(maxpoolLayer *layer, cortexSTATE state){ |
|||
uint32_t n = layer->m_nOut_Height*layer->m_nOut_Width*layer->m_nIn_Channels*layer->m_nBatch; |
|||
forward_maxpool_layer_kernel<<<gridSize(n), BLOCK>>>( |
|||
n, |
|||
layer->m_nIn_Height, |
|||
layer->m_nIn_Width, |
|||
layer->m_nIn_Channels, |
|||
layer->m_nStride, |
|||
layer->m_nSize, |
|||
layer->m_nPad, |
|||
state.input, |
|||
layer->m_pGpuOutput, |
|||
layer->m_pGpuIndexes |
|||
); |
|||
checkError();// cudaPeekAtLastError()); |
|||
}; |
|||
|
|||
//extern "C" void backward_maxpool_layer_gpu(maxpool_layer layer, network_state state) |
|||
void gpuCUDA::backward_layer(maxpoolLayer *layer, cortexSTATE state){ |
|||
unsigned int n = layer->m_nIn_Height*layer->m_nIn_Width*layer->m_nIn_Channels*layer->m_nBatch; |
|||
backward_maxpool_layer_kernel<<<gridSize(n), BLOCK>>>( |
|||
n, |
|||
layer->m_nIn_Height, |
|||
layer->m_nIn_Width, |
|||
layer->m_nIn_Channels, |
|||
layer->m_nStride, |
|||
layer->m_nSize, |
|||
layer->m_nPad, |
|||
layer->m_pGpuDelta, |
|||
state.delta, |
|||
layer->m_pGpuIndexes |
|||
); |
|||
checkError();// cudaPeekAtLastError()); |
|||
}; |
|||
|
|||
@ -0,0 +1,105 @@ |
|||
#include "coreLabel.h"
|
|||
sdlLabel::sdlLabel(int x, int y, int w, int h, sdlCoreWidget *wPtr, int fsize, CSTR fontName) :sdlCoreWidget(x,y,w,h,wPtr){ |
|||
m_sColorStyle = DEFAULT_COLOR_STYLE; |
|||
m_pFontTexture = NULL; |
|||
m_pFont = NULL; |
|||
m_sRect.x = x; |
|||
m_sRect.y = y; |
|||
m_sRect.w = w; |
|||
m_sRect.h = h; |
|||
setDecoraion(false); |
|||
m_bTextAlign = ALIGN_CENTER; |
|||
memset( m_pText, 0, SDL_DEFAULT_TEXT_SIZE ); |
|||
if (fontName != NULL){ |
|||
m_pFont = TTF_OpenFont(fontName, fsize); |
|||
} else { |
|||
m_pFont = TTF_OpenFont("C:\\FruchtSystems\\cortex_test\\cortex_test\\Fonts\\Ubuntu-L.ttf", fsize); |
|||
}; |
|||
m_bRedraw = true; |
|||
}; |
|||
|
|||
sdlLabel::~sdlLabel(){ |
|||
if (m_pFont != NULL){ |
|||
TTF_CloseFont(m_pFont); |
|||
m_pFont = NULL; |
|||
}; |
|||
if (m_pFontTexture != NULL){ |
|||
SDL_DestroyTexture(m_pFontTexture); |
|||
m_pFontTexture = NULL; |
|||
}; |
|||
}; |
|||
|
|||
void sdlLabel::draw(SDL_Renderer *pRenderer){ |
|||
if (m_bRedraw){ |
|||
if (m_pFont == NULL) return; |
|||
SDL_Surface *pSurface; |
|||
pSurface = TTF_RenderText_Solid(m_pFont, m_pText, m_sColor); |
|||
if (pSurface != NULL){ |
|||
m_nLabelWidth = pSurface->w; |
|||
m_nLabelHeight = pSurface->h; |
|||
m_pFontTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface); |
|||
if (m_pFontTexture != NULL) m_bRedraw = false; |
|||
}; |
|||
}; |
|||
if( m_pFontTexture != NULL ){ |
|||
SDL_Rect tRect; |
|||
switch (m_bTextAlign){ |
|||
case ALIGN_LEFT: tRect.x = 0; break; |
|||
case ALIGN_CENTER: tRect.x = (m_sRect.w - m_nLabelWidth) / 2; break; |
|||
case ALIGN_RIGHT: tRect.x = m_sRect.w - m_nLabelWidth; break; |
|||
}; |
|||
tRect.y = m_sRect.y + (m_sRect.h - m_nLabelHeight) / 2; |
|||
//tRect.y = (m_sRect.h - m_pSurface->h) / 2;
|
|||
tRect.w = m_nLabelWidth; |
|||
tRect.h = m_nLabelHeight; |
|||
//SDL_RenderCopyEx(pRenderer, m_pTexture, clip, &m_sRect, angle, center, flip);
|
|||
SDL_RenderCopyEx(pRenderer, m_pFontTexture, NULL, &tRect, 0, 0, SDL_FLIP_NONE); |
|||
}; |
|||
}; |
|||
|
|||
void sdlLabel::setText(CSTR pStr){ |
|||
if (pStr != NULL){ |
|||
sprintf(m_pText, "%s", pStr); |
|||
} else { |
|||
memset(m_pText, 0, SDL_DEFAULT_TEXT_SIZE ); |
|||
}; |
|||
m_bRedraw = true; |
|||
}; |
|||
|
|||
void sdlLabel::setFont(TTF_Font *tPtr){ |
|||
if (m_pFont != NULL){ |
|||
TTF_CloseFont(m_pFont); |
|||
m_pFont = NULL; |
|||
}; |
|||
m_pFont = tPtr; |
|||
m_bRedraw = true; |
|||
}; |
|||
|
|||
void sdlLabel::setColor(SDL_Color *tC){ |
|||
if (tC == NULL){ |
|||
m_sColor = m_sColorStyle.font; |
|||
} else { |
|||
memcpy(&m_sColor, tC, sizeof(SDL_Color)); |
|||
}; |
|||
m_bRedraw = true; |
|||
}; |
|||
void sdlLabel::setColor(Uint8 red, Uint8 green, Uint8 blue){ |
|||
//Modulate texture rgb
|
|||
if (m_pFontTexture != NULL){ |
|||
SDL_SetTextureColorMod(m_pFontTexture, red, green, blue); |
|||
}; |
|||
}; |
|||
|
|||
void sdlLabel::setBlendMode(SDL_BlendMode blending){ |
|||
//Set blending function
|
|||
if (m_pFontTexture != NULL){ |
|||
SDL_SetTextureBlendMode(m_pFontTexture, blending); |
|||
}; |
|||
}; |
|||
|
|||
void sdlLabel::setAlpha(Uint8 alpha){ |
|||
//Modulate texture alpha
|
|||
if (m_pFontTexture != NULL){ |
|||
SDL_SetTextureAlphaMod(m_pFontTexture, alpha); |
|||
}; |
|||
}; |
|||
@ -0,0 +1,43 @@ |
|||
#ifndef __SDL_CORE_LABEL_HEADERS__ |
|||
#define __SDL_CORE_LABEL_HEADERS__ |
|||
|
|||
#include "../../components/base/basicMutex.h" |
|||
#include "../gui_sdl.h" |
|||
#include "coreWidget.h" |
|||
|
|||
#define SDL_DEFAULT_TEXT_SIZE 1024 |
|||
#define SDL_DEFAULT_FONT_SIZE 12 |
|||
typedef enum E_TEXT_ALIGN{ |
|||
ALIGN_LEFT = 0, |
|||
ALIGN_CENTER = 1, |
|||
ALIGN_RIGHT |
|||
} TEXT_ALIGN; |
|||
|
|||
class sdlLabel :public sdlCoreWidget{ |
|||
private: |
|||
char m_pText[SDL_DEFAULT_TEXT_SIZE]; |
|||
TTF_Font * m_pFont; |
|||
SDL_Texture * m_pFontTexture; |
|||
SDL_Color m_sColor; |
|||
SDL_Rect m_sRect; |
|||
int m_nLabelWidth; |
|||
int m_nLabelHeight; |
|||
sdlColorStyle m_sColorStyle; |
|||
TEXT_ALIGN m_bTextAlign; |
|||
protected: |
|||
virtual void draw(SDL_Renderer *pRenderer); |
|||
public: |
|||
explicit sdlLabel(int x, int y, int w, int h, sdlCoreWidget *wPtr=NULL, int fsize = SDL_DEFAULT_FONT_SIZE, CSTR fontName = NULL); |
|||
virtual ~sdlLabel(); |
|||
void setText( CSTR pStr ); |
|||
void setFont( TTF_Font *tPtr ); |
|||
void setColor( SDL_Color *tC ); |
|||
void setColor( Uint8 red, Uint8 green, Uint8 blue ); |
|||
void setBlendMode(SDL_BlendMode blending ); |
|||
void setAlpha( Uint8 alpha ); |
|||
void setAlign(TEXT_ALIGN tA){ m_bTextAlign = tA; }; |
|||
CSTR getText(void){ return m_pText; }; |
|||
}; |
|||
|
|||
|
|||
#endif |
|||
@ -0,0 +1,32 @@ |
|||
#ifndef __SDL_CORE_STYLE_TEMPLATE_HEADER__ |
|||
#define __SDL_CORE_STYLE_TEMPLATE_HEADER__ |
|||
#include <stdint.h> |
|||
|
|||
typedef struct S_sdlColorStyle{ |
|||
SDL_Color panelBG; |
|||
SDL_Color borderTOP; |
|||
SDL_Color borderBOTTOM; |
|||
SDL_Color borderLEFT; |
|||
SDL_Color borderRIGHT; |
|||
SDL_Color borderBODY; |
|||
SDL_Color body; |
|||
SDL_Color font; |
|||
SDL_Color disabledFont; |
|||
SDL_Color activeSelector; |
|||
SDL_Color passiveSelector; |
|||
} sdlColorStyle; |
|||
|
|||
#define DEFAULT_COLOR_STYLE { \ |
|||
{0x20, 0x20, 0x20, 0xFF}, \ |
|||
{0xF0, 0xF0, 0xF0, 0xFF}, \ |
|||
{0xF0, 0xF0, 0xF0, 0xFF}, \ |
|||
{0xF0, 0xF0, 0xF0, 0xFF}, \ |
|||
{0xF0, 0xF0, 0xF0, 0xFF}, \ |
|||
{0xF0, 0xF0, 0xF0, 0xFF}, \ |
|||
{0x70, 0x70, 0x70, 0xFF}, \ |
|||
{0x0, 0x0, 0x0, 0xFF}, \ |
|||
{0x10, 0x10, 0x10, 0xFF}, \ |
|||
{0x10, 0x10, 0xF0, 0xFF}, \ |
|||
{0x0, 0x0, 0x20, 0xFF} \ |
|||
} |
|||
#endif |
|||
@ -0,0 +1,53 @@ |
|||
#include "coreWidget.h"
|
|||
|
|||
sdlCoreWidget::sdlCoreWidget(int x, int y, int width, int height, sdlCoreWidget * pParent){ |
|||
m_sRect.x = x; |
|||
m_sRect.y = y; |
|||
m_sRect.w = width; |
|||
m_sRect.h = height; |
|||
m_pParent = pParent; |
|||
m_bRedraw = true; |
|||
m_sColorStyle = DEFAULT_COLOR_STYLE; |
|||
m_pWidgetTexture = NULL; |
|||
m_bNoDecoration = false; |
|||
m_pGUI = classGUI_SDL::getInstance(); |
|||
if (m_pGUI != NULL) m_pGUI->registerWidget(this); |
|||
}; |
|||
|
|||
sdlCoreWidget::~sdlCoreWidget(){ |
|||
if (m_pGUI != NULL) m_pGUI->removeWidget(this); |
|||
if (m_pWidgetTexture != NULL) SDL_DestroyTexture(m_pWidgetTexture); |
|||
}; |
|||
|
|||
void sdlCoreWidget::render(SDL_Renderer *pRenderer){ |
|||
if (pRenderer == NULL) return; |
|||
if (m_pWidgetTexture == NULL) m_pWidgetTexture = SDL_CreateTexture(pRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, m_sRect.w, m_sRect.h); |
|||
if (m_pWidgetTexture == NULL) return; |
|||
if (m_bRedraw){ |
|||
SDL_SetRenderTarget(pRenderer, m_pWidgetTexture); |
|||
SDL_SetRenderDrawColor(pRenderer, 0x00, 0x00, 0x00, 0x00); |
|||
SDL_RenderFillRect(pRenderer, &m_sRect); |
|||
if (!m_bNoDecoration){ |
|||
SDL_SetRenderDrawColor(pRenderer, m_sColorStyle.panelBG.r, m_sColorStyle.panelBG.g, m_sColorStyle.panelBG.b, m_sColorStyle.panelBG.a); |
|||
SDL_RenderClear(pRenderer); |
|||
SDL_Rect r; |
|||
r.x = 0; r.y = 0; r.w = m_sRect.w; r.h = m_sRect.h; |
|||
SDL_SetRenderDrawColor(pRenderer, m_sColorStyle.borderBODY.r, m_sColorStyle.borderBODY.g, m_sColorStyle.borderBODY.b, m_sColorStyle.borderBODY.a); |
|||
SDL_RenderDrawRect(pRenderer, &r); |
|||
}; |
|||
draw( pRenderer ); |
|||
if (m_vWidgets.size()){ |
|||
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) (*index)->render(pRenderer); |
|||
}; |
|||
}; |
|||
if (m_pParent == NULL){ |
|||
SDL_SetRenderTarget(pRenderer, NULL); |
|||
} else { |
|||
SDL_SetRenderTarget(pRenderer, m_pParent->getWidgetTexture()); |
|||
}; |
|||
SDL_RenderCopy(pRenderer, m_pWidgetTexture, NULL, &m_sRect); |
|||
}; |
|||
|
|||
void sdlCoreWidget::setColorStyle(sdlColorStyle style){ |
|||
m_sColorStyle = style; |
|||
}; |
|||
@ -0,0 +1,33 @@ |
|||
#ifndef __SDL_CORE_WIDGET_HEADER__ |
|||
#define __SDL_CORE_WIDGET_HEADER__ |
|||
|
|||
#include "../gui_sdl.h" |
|||
#include "coreStyleTemplate.h" |
|||
|
|||
class sdlCoreWidget{ |
|||
protected: |
|||
sdlCoreWidget * m_pParent; |
|||
classGUI_SDL * m_pGUI; |
|||
SDL_Rect m_sRect; |
|||
sdlColorStyle m_sColorStyle; |
|||
pWidgetVector m_vWidgets; |
|||
SDL_Texture * m_pWidgetTexture; |
|||
bool m_bNoDecoration; |
|||
bool m_bRedraw; |
|||
void requerstUpdate(void); |
|||
virtual void draw(SDL_Renderer *pRenderer){}; |
|||
SDL_Texture * getWidgetTexture(void){ return m_pWidgetTexture; }; |
|||
public: |
|||
explicit sdlCoreWidget(int x, int y, int width, int height, sdlCoreWidget * pParent = NULL); |
|||
virtual ~sdlCoreWidget(); |
|||
void render(SDL_Renderer *pRenderer); |
|||
SDL_Rect * getBoundRect(void){ return &m_sRect; }; |
|||
virtual void dispatch(SDL_Event *pEvt){}; |
|||
void setColorStyle(sdlColorStyle style); |
|||
virtual void hide(){}; |
|||
void addWidget(sdlCoreWidget *tPtr); |
|||
void setDecoraion(bool A){ m_bNoDecoration = !A; }; |
|||
}; |
|||
|
|||
|
|||
#endif |
|||
@ -0,0 +1,193 @@ |
|||
#include "gui_sdl.h"
|
|||
#include "core/coreWidget.h"
|
|||
|
|||
classGUI_SDL * classGUI_SDL::m_pInstance = NULL; |
|||
classGUI_SDL * classGUI_SDL::getInstance(void){ |
|||
if (m_pInstance == NULL){ |
|||
m_pInstance = new classGUI_SDL(); |
|||
}; |
|||
return m_pInstance; |
|||
}; |
|||
|
|||
classGUI_SDL::classGUI_SDL(): basicThread(){ |
|||
m_pWindow = NULL; |
|||
m_pScreenSurface = NULL; |
|||
m_pTexture = NULL; |
|||
m_pRenderer = NULL; |
|||
m_bWindowSystemOk = false; |
|||
m_bInitOk = false; |
|||
m_pProtector = SDL_CreateMutex(); |
|||
int display_count = 0, display_index = 0, mode_index = 0; |
|||
SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 }; |
|||
if ((display_count = SDL_GetNumVideoDisplays()) < 1) { |
|||
SDL_Log("SDL_GetNumVideoDisplays returned: %i", display_count); |
|||
}; |
|||
SDL_Log("SDL_GetNumVideoDisplays returned: %i", display_count); |
|||
if (SDL_GetDisplayMode(display_index, mode_index, &mode) != 0) { |
|||
SDL_Log("SDL_GetDisplayMode failed: %s", SDL_GetError()); |
|||
}; |
|||
SDL_Log("SDL_GetDisplayMode(0, 0, &mode):\t\t%i bpp\t%i x %i", SDL_BITSPERPIXEL(mode.format), mode.w, mode.h ); |
|||
m_bInitOk = basicThread::init(); |
|||
while ( ( m_bWindowSystemOk == false ) && ( m_bThreadStatus < TH_ENDED ) ) SLEEP(1); |
|||
}; |
|||
|
|||
classGUI_SDL::~classGUI_SDL(){ |
|||
m_bWindowSystemOk = false; |
|||
m_bInitOk = false; |
|||
SDL_DestroyMutex( m_pProtector ); |
|||
if (m_pRenderer!=NULL){ |
|||
SDL_DestroyRenderer(m_pRenderer); |
|||
}; |
|||
if (m_pScreenSurface != NULL){ |
|||
//Deallocate surface
|
|||
SDL_FreeSurface(m_pScreenSurface); |
|||
m_pScreenSurface = NULL; |
|||
}; |
|||
if ( m_pWindow!= NULL ){ |
|||
//Destroy window
|
|||
SDL_DestroyWindow(m_pWindow); |
|||
m_pWindow = NULL; |
|||
}; |
|||
//Quit SDL subsystems
|
|||
SDL_Quit(); |
|||
}; |
|||
|
|||
|
|||
|
|||
//https://www.facebook.com/missing.person.3158
|
|||
//https://www.facebook.com/guga.wereteli.90
|
|||
//https://www.facebook.com/mentul.ohmentul
|
|||
//https://www.facebook.com/profile.php?id=100001772172980
|
|||
//https://www.facebook.com/egagua
|
|||
/* Collect Confirmation Codes
|
|||
Contact your friends and ask them for their help. |
|||
1. Call three of these friends or talk to them in person. |
|||
Raphael LlamadoChantebis SamkaroMentul OhmentulEstrada Irish PotchEvgeny Gagua |
|||
2. Ask them to visit : |
|||
https ://www.facebook.com/recover
|
|||
3. Each friend will give you a code.Enter them below : |
|||
*/ |
|||
void classGUI_SDL::m_fThreadProc(void){ |
|||
if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ){ |
|||
m_bInitOk = false; |
|||
m_bThreadStatus = TH_ENDED; |
|||
return; |
|||
}; |
|||
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)){ |
|||
//printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
|
|||
m_bInitOk = false; |
|||
m_bThreadStatus = TH_ENDED; |
|||
return; |
|||
}; |
|||
|
|||
//Initialize SDL_ttf
|
|||
if( TTF_Init() == -1 ){ |
|||
//printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
|
|||
m_bInitOk = false; |
|||
m_bThreadStatus = TH_ENDED; |
|||
return; |
|||
} else { |
|||
m_bInitOk = true; |
|||
if (SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE, &m_pWindow, &m_pRenderer)){ |
|||
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); |
|||
m_bThreadStatus = TH_ENDED; |
|||
m_bWindowSystemOk = false; |
|||
return; |
|||
} else { |
|||
//SDL_SetWindowOpacity(m_pWindow, 0.5);
|
|||
//Get window surface
|
|||
//m_pScreenSurface = SDL_GetWindowSurface(m_pWindow);
|
|||
m_bThreadStatus = TH_WORKING; |
|||
m_bWindowSystemOk = true; |
|||
}; |
|||
}; |
|||
while( m_bThreadStatus < TH_ENDED ){ |
|||
//Event handler
|
|||
SDL_Event mEvent; |
|||
//Handle events on queue
|
|||
if( SDL_PollEvent(&mEvent) != 0 ){ |
|||
//m_mProtector.lock();
|
|||
SDL_LockMutex(m_pProtector); |
|||
if (m_vWidgets.size()){ |
|||
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) (*index)->dispatch(&mEvent); |
|||
}; |
|||
//m_mProtector.unlock();
|
|||
SDL_UnlockMutex(m_pProtector); |
|||
// Global Window Event handler
|
|||
if( mEvent.type == SDL_QUIT ){ |
|||
m_bThreadStatus = TH_ENDED; |
|||
} else if (mEvent.type == SDL_KEYDOWN){ //User presses a key
|
|||
//Select surfaces based on key press
|
|||
switch( mEvent.key.keysym.sym ){ |
|||
case SDLK_UP: |
|||
//gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_UP];
|
|||
break; |
|||
case SDLK_DOWN: |
|||
//gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DOWN];
|
|||
break; |
|||
case SDLK_LEFT: |
|||
//gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_LEFT];
|
|||
break; |
|||
case SDLK_RIGHT: |
|||
//gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_RIGHT];
|
|||
break; |
|||
default: |
|||
//gCurrentSurface = gKeyPressSurfaces[KEY_PRESS_SURFACE_DEFAULT];
|
|||
break; |
|||
}; |
|||
}; |
|||
}; |
|||
//m_mProtector.lock();
|
|||
SDL_LockMutex(m_pProtector); |
|||
if (m_vWidgets.size()){ |
|||
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++) (*index)->render(m_pRenderer); |
|||
SDL_RenderPresent(m_pRenderer); |
|||
}; |
|||
//m_mProtector.unlock();
|
|||
SDL_UnlockMutex(m_pProtector); |
|||
//Apply the image
|
|||
//SDL_BlitSurface(gXOut, NULL, m_pScreenSurface, NULL);
|
|||
//Update the surface
|
|||
//SDL_UpdateWindowSurface(m_pWindow);
|
|||
//SLEEP(150);
|
|||
}; |
|||
}; |
|||
//FOXCONN PVB120G12H-P01
|
|||
//AVC MD413 BA13733B12H
|
|||
|
|||
void classGUI_SDL::registerWidget(sdlCoreWidget * tPtr){ |
|||
while( !m_bWindowSystemOk ) SLEEP(1); |
|||
if( tPtr != NULL ){ |
|||
//m_mProtector.lock();
|
|||
SDL_LockMutex(m_pProtector); |
|||
m_vWidgets.push_back(tPtr); |
|||
//m_mProtector.unlock();
|
|||
SDL_UnlockMutex(m_pProtector); |
|||
}; |
|||
}; |
|||
|
|||
void classGUI_SDL::removeWidget(sdlCoreWidget * tPtr){ |
|||
if (tPtr != NULL){ |
|||
//m_mProtector.lock();
|
|||
SDL_LockMutex(m_pProtector); |
|||
for (pWidgetVector::iterator index = m_vWidgets.begin(); index < m_vWidgets.end(); index++){ |
|||
if ((*index) == tPtr){ |
|||
(*index)->hide(); |
|||
m_vWidgets.erase(index); |
|||
break; |
|||
}; |
|||
}; |
|||
//m_mProtector.unlock();
|
|||
SDL_UnlockMutex(m_pProtector); |
|||
}; |
|||
}; |
|||
|
|||
bool classGUI_SDL::setFullScreen( bool status ){ |
|||
if (m_pWindow == NULL) return false; |
|||
if (status){ |
|||
SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN ); |
|||
} else { |
|||
SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN_DESKTOP); |
|||
}; |
|||
return true; |
|||
}; |
|||
@ -0,0 +1,42 @@ |
|||
#ifndef __GUI_SDL_HEADER__ |
|||
#define __GUI_SDL_HEADER__ |
|||
|
|||
#include <stdio.h> |
|||
#include <vector> |
|||
#include "../components/base/basicMutex.h" |
|||
#include "../components/base/basicThread.h" |
|||
#include <SDL.h> |
|||
#include <SDL_image.h> |
|||
#include <SDL_ttf.h> |
|||
|
|||
#define SCREEN_WIDTH 800 |
|||
#define SCREEN_HEIGHT 600 |
|||
|
|||
class sdlCoreWidget; |
|||
|
|||
typedef std::vector<sdlCoreWidget*> pWidgetVector; |
|||
|
|||
class classGUI_SDL: public basicThread{ |
|||
private: |
|||
bool m_bInitOk; |
|||
bool m_bWindowSystemOk; |
|||
SDL_Window * m_pWindow; |
|||
SDL_Renderer * m_pRenderer; |
|||
SDL_Surface * m_pScreenSurface; |
|||
SDL_Texture * m_pTexture; |
|||
//basicMutex m_mProtector; |
|||
SDL_mutex * m_pProtector; |
|||
pWidgetVector m_vWidgets; |
|||
public: |
|||
static classGUI_SDL * getInstance(); |
|||
virtual void m_fThreadProc(void); |
|||
void registerWidget(sdlCoreWidget * tPtr); |
|||
void removeWidget( sdlCoreWidget * tPtr); |
|||
bool setFullScreen( bool status ); |
|||
protected: |
|||
classGUI_SDL(); |
|||
~classGUI_SDL(); |
|||
static classGUI_SDL * m_pInstance; |
|||
}; |
|||
|
|||
#endif |
|||
@ -0,0 +1,59 @@ |
|||
#include "logWidget.h"
|
|||
|
|||
sdlLog::sdlLog(int x, int y, int width, int height, sdlCoreWidget * pWnd) :sdlCoreWidget(x, y, width, height, pWnd){ |
|||
m_nLines = height / SDL_DEFAULT_LOG_ROW_SIZE; |
|||
m_nCurrentLine = 0; |
|||
m_ppLines = (sdlLabel**)calloc( m_nLines, sizeof(sdlLabel*) ); |
|||
memset( m_ppLines, 0, m_nLines * sizeof(sdlLabel*) ); |
|||
}; |
|||
|
|||
sdlLog::~sdlLog(){ |
|||
if (m_ppLines != NULL){ |
|||
for (int index = 0; index < m_nLines; index++) delete(m_ppLines[index]); |
|||
free(m_ppLines); |
|||
}; |
|||
}; |
|||
|
|||
void sdlLog::logMessage(const char *Msg){ |
|||
if (m_ppLines != NULL){ |
|||
//m_mProtector.lock();
|
|||
if (m_nCurrentLine < m_nLines){ |
|||
m_ppLines[m_nCurrentLine] = new sdlLabel( |
|||
1, |
|||
1 + (m_nCurrentLine*SDL_DEFAULT_LOG_ROW_SIZE), |
|||
m_sRect.w - 2, |
|||
SDL_DEFAULT_LOG_ROW_SIZE, |
|||
this, |
|||
SDL_DEFAULT_LOG_FONT_SIZE |
|||
); |
|||
if( m_ppLines[m_nCurrentLine] != NULL ){ |
|||
m_ppLines[m_nCurrentLine]->setAlign(ALIGN_LEFT); |
|||
m_nCurrentLine++; |
|||
}; |
|||
for (int index = m_nCurrentLine - 1; index > 0; index--){ |
|||
char tBuf[SDL_DEFAULT_TEXT_SIZE]; |
|||
memcpy(tBuf, m_ppLines[index - 1]->getText(), SDL_DEFAULT_TEXT_SIZE); |
|||
m_ppLines[index]->setText(tBuf); |
|||
}; |
|||
m_ppLines[0]->setText((CSTR)Msg); |
|||
} else { |
|||
for (int index = m_nLines - 1; index > 0; index-- ){ |
|||
char tBuf[SDL_DEFAULT_TEXT_SIZE]; |
|||
memcpy(tBuf, m_ppLines[index-1]->getText(), SDL_DEFAULT_TEXT_SIZE); |
|||
m_ppLines[index]->setText(tBuf); |
|||
}; |
|||
m_ppLines[0]->setText((CSTR)Msg); |
|||
}; |
|||
//m_mProtector.unlock();
|
|||
}; |
|||
}; |
|||
|
|||
void sdlLog::logMessage(CSTR Msg){ |
|||
logMessage((const char*)Msg); |
|||
}; |
|||
|
|||
void sdlLog::draw(SDL_Renderer *pRenderer){ |
|||
for (int index = 0; index < m_nLines; index++){ |
|||
if (m_ppLines[index] != NULL) m_ppLines[index]->render(pRenderer); |
|||
}; |
|||
}; |
|||
@ -0,0 +1,29 @@ |
|||
#ifndef __SDL_LOG_WIDGET_HEADER__ |
|||
#define __SDL_LOG_WIDGET_HEADER__ |
|||
#include "../core/coreWidget.h" |
|||
#include "../core/coreLabel.h" |
|||
#include "../../components/base/basicMutex.h" |
|||
#include "../../components/base/basicLoggingService.h" |
|||
|
|||
#define SDL_DEFAULT_LOG_FONT_SIZE 10 |
|||
#define SDL_DEFAULT_LOG_ROW_SIZE 12 |
|||
|
|||
class sdlLog :public basicLoggingService, public sdlCoreWidget { |
|||
private: |
|||
sdlLabel ** m_ppLines; |
|||
int m_nLines; |
|||
int m_nCurrentLine; |
|||
basicMutex m_mProtector; |
|||
protected: |
|||
virtual void draw(SDL_Renderer *pRenderer); |
|||
public: |
|||
explicit sdlLog(int x, int y, int width, int height, sdlCoreWidget * pWnd = NULL); |
|||
virtual ~sdlLog(); |
|||
virtual void logMessage(CSTR Msg); |
|||
virtual void logMessage(const char *Msg); |
|||
}; |
|||
|
|||
|
|||
|
|||
|
|||
#endif |
|||
@ -0,0 +1,59 @@ |
|||
#include "progressBar.h"
|
|||
sdlProgressBar::sdlProgressBar(int x, int y, int width, int height, sdlCoreWidget * pWnd) :sdlCoreWidget(x, y, width, height, pWnd){ |
|||
m_pStatusProvider = NULL; |
|||
m_pLabel = NULL; |
|||
}; |
|||
|
|||
sdlProgressBar::~sdlProgressBar(){ |
|||
|
|||
}; |
|||
|
|||
void sdlProgressBar::draw(SDL_Renderer *pRenderer){ |
|||
m_mStatusProtecter.lock(); |
|||
if( m_pStatusProvider ){ |
|||
SDL_Rect r; |
|||
char tBuff[256]; |
|||
uint8_t bStatus; |
|||
bStatus = m_pStatusProvider->getStatus(tBuff); |
|||
if (tBuff[0]!=(char)0)setText(tBuff); |
|||
if (bStatus > 0){ |
|||
SDL_SetRenderDrawColor(pRenderer, m_sColorStyle.activeSelector.r, m_sColorStyle.activeSelector.g, m_sColorStyle.activeSelector.b, m_sColorStyle.activeSelector.a); |
|||
if (bStatus < 100){ |
|||
r.x = 1; r.y = 1; r.w = ( m_sRect.w * bStatus ) / 100 -2; r.h = m_sRect.h - 2; |
|||
} else { |
|||
r.x = 1; r.y = 1; r.w = m_sRect.w - 2; r.h = m_sRect.h - 2; |
|||
}; |
|||
//SDL_RenderDrawRect(pRenderer, &r);
|
|||
SDL_RenderFillRect(pRenderer, &r); |
|||
|
|||
if (m_pLabel != NULL) m_pLabel->render(pRenderer); |
|||
//SDL_SetRenderDrawColor(pRenderer, m_sColorStyle.borderBODY.r, m_sColorStyle.borderBODY.g, m_sColorStyle.borderBODY.b, m_sColorStyle.borderBODY.a);
|
|||
//r.x = 0; r.y = 0; r.w = m_sRect.w; r.h = m_sRect.h;
|
|||
//SDL_RenderDrawRect(pRenderer, &r);
|
|||
}; |
|||
}; |
|||
m_mStatusProtecter.unlock(); |
|||
}; |
|||
|
|||
void sdlProgressBar::dispatch(SDL_Event *pEvt){ |
|||
if (pEvt->type == SDL_MOUSEMOTION || pEvt->type == SDL_MOUSEBUTTONDOWN || pEvt->type == SDL_MOUSEBUTTONUP){ |
|||
//Get mouse position
|
|||
int x, y; |
|||
SDL_GetMouseState(&x, &y); |
|||
}; |
|||
}; |
|||
|
|||
void sdlProgressBar::attachStatusProvider(basicStatusProvider *tPtr){ |
|||
m_mStatusProtecter.lock(); |
|||
m_pStatusProvider = tPtr; |
|||
m_mStatusProtecter.unlock(); |
|||
}; |
|||
|
|||
void sdlProgressBar::setText(CSTR tPtr){ |
|||
if (m_pLabel == NULL){ |
|||
m_pLabel = new sdlLabel( 1, 1, m_sRect.w - 2, m_sRect.h - 2, this, 12 ); |
|||
}; |
|||
if (m_pLabel != NULL){ |
|||
m_pLabel->setText(tPtr); |
|||
}; |
|||
}; |
|||
@ -0,0 +1,23 @@ |
|||
#ifndef __GUI_SDL_WIDGET_PROGRESS_BAR_HEADER__ |
|||
#define __GUI_SDL_WIDGET_PROGRESS_BAR_HEADER__ |
|||
#include "../core/coreWidget.h" |
|||
#include "../core/coreLabel.h" |
|||
#include "../../components/base/basicMutex.h" |
|||
#include "../../components/base/basicStatusProvider.h" |
|||
|
|||
class sdlProgressBar : public sdlCoreWidget{ |
|||
private: |
|||
basicStatusProvider * m_pStatusProvider; |
|||
sdlLabel * m_pLabel; |
|||
basicMutex m_mStatusProtecter; |
|||
protected: |
|||
virtual void draw(SDL_Renderer *pRenderer); |
|||
public: |
|||
explicit sdlProgressBar(int x, int y, int width, int height, sdlCoreWidget * pWnd = NULL); |
|||
virtual ~sdlProgressBar(); |
|||
void attachStatusProvider(basicStatusProvider *tPtr); |
|||
virtual void dispatch(SDL_Event *pEvt); |
|||
void setText( CSTR tPtr ); |
|||
}; |
|||
|
|||
#endif |
|||
@ -1,4 +1,4 @@ |
|||
#!/bin/bash |
|||
|
|||
#git commit -a -m '$1' |
|||
git commit -a -m 'PRU changed tp work with each Mirror and LRF separately' |
|||
git commit -a -m 'Cortex Library Updated to last MERCURY version Also added SDL preliminary basics' |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue