-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeanSquaredError.cu
More file actions
72 lines (61 loc) · 1.79 KB
/
MeanSquaredError.cu
File metadata and controls
72 lines (61 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "MeanSquaredError.h"
#include <iostream>
#include <math.h>
using namespace std;
void __global__ gpuCost(float* d_returnCost, float* d_output, float* d_correctOutput, int size)
{
int x = blockIdx.x*blockDim.x + threadIdx.x;
if(x<size)
d_returnCost[x] = (d_correctOutput[x] - d_output[x]) * (d_correctOutput[x] - d_output[x]) * 0.5;
}
void __global__ gpuError(float* d_returnError, float* d_output, float* d_correctOutput, int size)
{
int x = blockIdx.x*blockDim.x + threadIdx.x;
if(x<size)
d_returnError[x] = d_correctOutput[x] - d_output[x];
}
MeanSquaredError::MeanSquaredError()
{
this->gpuLoaded = false;
}
MeanSquaredError::~MeanSquaredError()
{
}
void MeanSquaredError::cost(float* returnCost, float* output, float* correctOutput, int aSize)
{
if(!gpuLoaded){
for(int i=0; i<aSize; i++)
{
returnCost[i] = (correctOutput[i] - output[i]) * (correctOutput[i] - output[i]) * 0.5;
}
}else{
int threadsPerBlock = 1024;
int blocks = (aSize + threadsPerBlock) / threadsPerBlock;
gpuCost<<<blocks,threadsPerBlock>>>(returnCost, output, correctOutput, aSize);
}
}
void MeanSquaredError::error(float* returnError, float* output, float* correctOutput, int aSize)
{
if(!gpuLoaded){
for(int i=0; i<aSize; i++)
{
returnError[i] = correctOutput[i] - output[i];
}
}else{
int threadsPerBlock = 1024;
int blocks = (aSize + threadsPerBlock) / threadsPerBlock;
gpuError<<<blocks,threadsPerBlock>>>(returnError, output, correctOutput, aSize);
}
}
void MeanSquaredError::loadGPU()
{
this->gpuLoaded = true;
}
void MeanSquaredError::unloadGPU()
{
this->gpuLoaded = false;
}
string MeanSquaredError::toString()
{
return "Mean Squared Error";
}