/
VSNR_ADMM_2D_GPU_SINGLE.cu
415 lines (337 loc) · 12.6 KB
/
VSNR_ADMM_2D_GPU_SINGLE.cu
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// VSNR ADMM 2D ON GPU WITH MATLAB
#include <math.h>
#include "mex.h"
#include "gpu/mxGPUArray.h"
#include "cuda.h"
#include "cuda_runtime.h"
#include "cufft.h"
// Computes out=u1.*u2
__global__ void product_carray(cufftComplex *u1,cufftComplex *u2,cufftComplex *out,int n){
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
out[i].x=u1[i].x*u2[i].x-u1[i].y*u2[i].y;
out[i].y=u1[i].y*u2[i].x+u1[i].x*u2[i].y;
i+=blockDim.x*gridDim.x;
}
}
// Normalize an array
__global__ void normalize( cufftReal* u, int n) {
// indices of the thread
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
u[i]=u[i]/n;
i+=blockDim.x*gridDim.x;
}
}
// Adds two vectors w=u+v
__global__ void Add( float* u, float *v, float *w, int n) {
// indices of the thread
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
w[i]=u[i]+v[i];
i+=blockDim.x*gridDim.x;
}
}
// Substracts two vectors w=u-v
__global__ void Substract( float* u, float *v, float *w, int n) {
// indices of the thread
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
w[i]=u[i]-v[i];
i+=blockDim.x*gridDim.x;
}
}
// Sets finite difference 1
__global__ void setd1( float* d1, int n, int n1) {
// indices of the thread
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
if (i==0) {d1[i]=1;}
else if (i==n1-1) {d1[i]=-1;}
else {d1[i]=0;}
i+=blockDim.x*gridDim.x;
}
}
// Sets finite difference 2
__global__ void setd2( float* d2, int n, int n1) {
// indices of the thread
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
if (i==0) {d2[i]=1;}
else if (i==n-n1) {d2[i]=-1;}
else {d2[i]=0;}
i+=blockDim.x*gridDim.x;
}
}
// Compute Phi
__global__ void Compute_Phi(cufftComplex* fphi1, cufftComplex* fphi2,cufftComplex *fphi, float beta, int n) {
int i=0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
fphi[i].x=1 + beta*(fphi1[i].x*fphi1[i].x + fphi1[i].y*fphi1[i].y + fphi2[i].x*fphi2[i].x + fphi2[i].y*fphi2[i].y);
fphi[i].y=0;
i+=blockDim.x*gridDim.x;
}
}
// Computes tmpi=-lambdai+beta*yi
__global__ void betay_m_lambda(cufftReal* lambda1,cufftReal* lambda2,cufftReal* y1,cufftReal* y2,cufftReal* tmp1,cufftReal* tmp2, float beta,int n) {
// indices of the thread
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
tmp1[i]=-lambda1[i]+beta*y1[i];
tmp2[i]=-lambda2[i]+beta*y2[i];
i+=blockDim.x*gridDim.x;
}
}
// Computes w=conj(u)*v
__global__ void conju_x_v(cufftComplex* u,cufftComplex* v,cufftComplex* w,int n){
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
float a1,a2,b1,b2;
while (i < n){
a1=u[i].x;b1=u[i].y;
a2=v[i].x;b2=v[i].y;
w[i].x=a1*a2+b1*b2;
w[i].y=b2*a1-b1*a2;
i+=blockDim.x*gridDim.x;
}
}
// fx=(tmp1+tmp2)/fphi;
__global__ void updatefx(cufftComplex* ftmp1,cufftComplex* ftmp2,cufftComplex* fphi,cufftComplex* fx, int n){
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
fx[i].x=(ftmp1[i].x+ftmp2[i].x)/fphi[i].x;
fx[i].y=(ftmp1[i].y+ftmp2[i].y)/fphi[i].x;
i+=blockDim.x*gridDim.x;
}
}
__global__ void updatey(cufftReal* d1u0,cufftReal* d2u0,cufftReal* tmp1,cufftReal* tmp2,cufftReal* lambda1,cufftReal* lambda2,cufftReal* y1,cufftReal* y2,float beta,int n){
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
float ng,t1,t2;
while (i < n){
t1=d1u0[i]-(tmp1[i]+lambda1[i]/beta);
t2=d2u0[i]-(tmp2[i]+lambda2[i]/beta);
ng=sqrt(t1*t1+t2*t2);
if (ng>1.0/beta){
y1[i]=d1u0[i]-t1*(1.0-1.0/(beta*ng));
y2[i]=d2u0[i]-t2*(1.0-1.0/(beta*ng));
}
else {
y1[i]=d1u0[i];
y2[i]=d2u0[i];
}
i+=blockDim.x*gridDim.x;
}
}
__global__ void updatelambda(cufftReal* lambda,cufftReal* tmp,cufftReal* y,float beta,int n){
int i = 0;
i = blockIdx.x * blockDim.x + threadIdx.x ;
while (i < n){
lambda[i]=lambda[i]+beta*(tmp[i]-y[i]);
i+=blockDim.x*gridDim.x;
}
}
// Displays a real array as a vector
void disp_array2(float *u,int n){
float* copy_u = (float*)malloc(n*sizeof(float));
cudaMemcpy(copy_u, u, n*sizeof(float), cudaMemcpyDeviceToHost);
for (int i=0;i<n;++i){
printf("%1.4f ",copy_u[i]);
}
printf("\n\n");
free(copy_u);
}
// Displays a complex array as a vector
void disp_carray2(cufftComplex *u,int n){
float2* copy_u = (float2*)malloc(n*sizeof(float2));
cudaMemcpy(copy_u, u, n*sizeof(float2), cudaMemcpyDeviceToHost);
for (int i=0;i<n;++i){
printf("%1.4f+i%1.4f ",copy_u[i].x,copy_u[i].y);
}
printf("\n\n");
free(copy_u);
}
// Main function
void VSNR_ADMM_GPU(float *u0,float *psi, int n0, int n1,int nit,float beta,float *u,int dimGrid, int dimBlock){
cufftHandle plan_R2C,plan_C2R;
cufftComplex *fpsi,*fu0;
cufftComplex *fd1,*fd2,*fphi1,*fphi2,*fphi,*ftmp1,*ftmp2,*fx;
cufftReal *d1,*d2,*d1u0,*d2u0,*tmp1,*tmp2,*y1,*y2,*lambda1,*lambda2;
int n=n0*n1;
int n_=n0*(n1/2+1);
printf("VSNR2D - ADMM - GPU \n");
cudaMalloc((void**)&fpsi,sizeof(cufftComplex)*n_);
cudaMalloc((void**)&fu0,sizeof(cufftComplex)*n_);
cudaMalloc((void**)&d1u0,sizeof(cufftReal)*n);
cudaMalloc((void**)&d2u0,sizeof(cufftReal)*n);
// Allocation for the main loop
cudaMalloc((void**)&tmp1,sizeof(cufftReal)*n);
cudaMalloc((void**)&tmp2,sizeof(cufftReal)*n);
cudaMalloc((void**)&ftmp1,sizeof(cufftComplex)*n_);
cudaMalloc((void**)&ftmp2,sizeof(cufftComplex)*n_);
cufftPlan2d(&plan_R2C,n0,n1,CUFFT_R2C);
cufftPlan2d(&plan_C2R,n0,n1,CUFFT_C2R);
cufftExecR2C(plan_R2C,u0,fu0); // fu0=fftn(u0)
cufftExecR2C(plan_R2C,psi,fpsi); // fpsi=fftn(psi)
// Computes d1 and fd1
cudaMalloc((void**)&d1,sizeof(cufftReal)*n);
cudaMalloc((void**)&fd1,sizeof(cufftComplex)*n_);
setd1<<<dimGrid,dimBlock>>>( d1, n, n1); //d1[0]=1;d1[n1-1]=-1;
cufftExecR2C(plan_R2C,d1,fd1);
cudaFree(d1);
// Computes d2 and fd2
cudaMalloc((void**)&d2,sizeof(cufftReal)*n);
cudaMemset(d2,0,sizeof(cufftReal)*n);
cudaMalloc((void**)&fd2,sizeof(cufftComplex)*n_);
setd2<<<dimGrid,dimBlock>>>( d2, n, n1); //d2[0]=1;d2[(n0-1)*n1]=-1;
cufftExecR2C(plan_R2C,d2,fd2);
cudaFree(d2);
// Computes d1u0
product_carray<<<dimGrid,dimBlock>>>(fd1,fu0,ftmp1,n_);
cufftExecC2R(plan_C2R,ftmp1,d1u0); // d1u0=ifftn(fd1.*fu0)
normalize<<<dimGrid,dimBlock>>>(d1u0,n);
// Computes d2u0
product_carray<<<dimGrid,dimBlock>>>(fd2,fu0,ftmp2,n_);
cufftExecC2R(plan_C2R,ftmp2,d2u0); // d2u0=ifftn(fd2.*fu0)
normalize<<<dimGrid,dimBlock>>>(d2u0,n);
cudaFree(fu0); // This is unused until the end
// Computes fphi1 and fphi2
cudaMalloc((void**)&fphi1,sizeof(cufftComplex)*n_);
cudaMalloc((void**)&fphi2,sizeof(cufftComplex)*n_);
product_carray<<<dimGrid,dimBlock>>>(fd1,fpsi,fphi1,n_); //fphi1=fpsi.*fd1
product_carray<<<dimGrid,dimBlock>>>(fd2,fpsi,fphi2,n_); //fphi2=fpsi.*fd2
//disp_carray2(fpsi,n_);
//disp_array2(psi,n);
//disp_carray2(fd2,n_);
cudaFree(fd1);
cudaFree(fd2);
// Computes fphi
cudaMalloc((void**)&fphi,sizeof(cufftComplex) *n_);
Compute_Phi<<<dimGrid,dimBlock>>>(fphi1, fphi2, fphi,beta,n_);
// Initialization
cudaMalloc((void**)&y1,sizeof(cufftReal)*n);
cudaMemset(y1,0,sizeof(cufftReal)*n);
cudaMalloc((void**)&y2,sizeof(cufftReal)*n);
cudaMemset(y2,0,sizeof(cufftReal)*n);
cudaMalloc((void**)&lambda1,sizeof(cufftReal)*n);
cudaMemset(lambda1,0,sizeof(cufftReal)*n);
cudaMalloc((void**)&lambda2,sizeof(cufftReal)*n);
cudaMemset(lambda2,0,sizeof(cufftReal)*n);
cudaMalloc((void**)&fx,sizeof(cufftComplex)*n_);
// Main algorithm
for (int k=0;k<nit;++k){
///////////////////////////////////////////////////////////
// First step, x update : (I+beta ATA)x = AT (-lambda+beta*ATy)
///////////////////////////////////////////////////////////
// ftmp1=conj(fphi1).*(fftn(-lambda1+beta*y1));
// ftmp2=conj(fphi2).*(fftn(-lambda2+beta*y2));
betay_m_lambda<<<dimGrid,dimBlock>>>(lambda1,lambda2,y1,y2,tmp1,tmp2,beta,n);
cufftExecR2C(plan_R2C,tmp1,ftmp1);
cufftExecR2C(plan_R2C,tmp2,ftmp2);
conju_x_v<<<dimGrid,dimBlock>>>(fphi1,ftmp1,ftmp1,n_);
conju_x_v<<<dimGrid,dimBlock>>>(fphi2,ftmp2,ftmp2,n_);
updatefx<<<dimGrid,dimBlock>>>(ftmp1,ftmp2,fphi,fx,n_);
///////////////////////////////////////////////////////////
// Second step y update : y=prox_{f1/beta}(Ax+lambda/beta)
///////////////////////////////////////////////////////////
product_carray<<<dimGrid,dimBlock>>>(fphi1,fx,ftmp1,n_);
product_carray<<<dimGrid,dimBlock>>>(fphi2,fx,ftmp2,n_);
cufftExecC2R(plan_C2R,ftmp1,tmp1); // tmp1 = Ax1
normalize<<<dimGrid,dimBlock>>>(tmp1,n);
cufftExecC2R(plan_C2R,ftmp2,tmp2); // tmp2 = Ax2
normalize<<<dimGrid,dimBlock>>>(tmp2,n);
updatey<<<dimGrid,dimBlock>>>(d1u0,d2u0,tmp1,tmp2,lambda1,lambda2,y1,y2,beta,n);
///////////////////////////////////////////////////////////
// Third step lambda update
///////////////////////////////////////////////////////////
updatelambda<<<dimGrid,dimBlock>>>(lambda1,tmp1,y1,beta,n);
updatelambda<<<dimGrid,dimBlock>>>(lambda2,tmp2,y2,beta,n);
}
// Last but not the least : u=u0-psi*x
product_carray<<<dimGrid,dimBlock>>>(fx,fpsi,ftmp1,n_);
cufftExecC2R(plan_C2R,ftmp1,u);
normalize<<<dimGrid,dimBlock>>>(u,n);
Substract<<<dimGrid,dimBlock>>>(u0,u,u,n);
// Free memory
cudaFree(fpsi);
cudaFree(fphi);
cudaFree(fphi1);
cudaFree(fphi2);
cudaFree(ftmp1);
cudaFree(ftmp2);
cudaFree(fx);
cudaFree(d1u0);
cudaFree(d2u0);
cudaFree(y1);
cudaFree(y2);
cudaFree(lambda1);
cudaFree(lambda2);
cudaFree(tmp1);
cudaFree(tmp2);
cufftDestroy(plan_R2C);
cufftDestroy(plan_C2R);
}
// Entry point for Matlab
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
// Ouput : u
// Input : u0, psi, nit, beta, dimGird, dimBlock
int n0,n1,nit,dimGrid,dimBlock;
float beta;
mwSize const *dim;
mxGPUArray *gu0,*gpsi;
float *u0,*psi;
mxGPUArray *gu;
float *u;
char const * const errId = "parallel:gpu:VSNR_ADMM_2D_GPU_SINGLE:InvalidInput";
char const * const errMsg = "Invalid input to MEX file. Should be SINGLE gpuArray.";
// Initialize the MathWorks GPU API.
mxInitGPU();
// Check for proper input
switch(nrhs) {
case 6 : /*mexPrintf("Good call.\n");*/
break;
default: mexErrMsgTxt("Bad number of inputs.\n");
break;
}
if (nlhs > 1) {mexErrMsgTxt("Too many outputs.\n");}
if (!(mxIsGPUArray(prhs[0]))) mexErrMsgIdAndTxt(errId, errMsg);
if (!(mxIsGPUArray(prhs[1]))) mexErrMsgIdAndTxt(errId, errMsg);
// Get input arguments
gu0 = mxGPUCopyFromMxArray(prhs[0]); // Here I would prefer using mxGPUCreateFromMxArray to avoid a copy, but this leads to new complications: I get a const * that messes up all subsequent functions
gpsi= mxGPUCopyFromMxArray(prhs[1]);
nit=(int)*mxGetPr(prhs[2]);
beta=*mxGetPr(prhs[3]);
dimGrid=(int)*mxGetPr(prhs[4]);
dimBlock=(int)*mxGetPr(prhs[5]);
// Note that n0 and n1 are reversed because of row major in C VS column major format in Matlab
dim = mxGPUGetDimensions(gu0);
n1=dim[0]; //number of rows
n0=dim[1]; //number of columns
if (mxGPUGetClassID(gu0) != mxSINGLE_CLASS) mexErrMsgIdAndTxt(errId, errMsg);
if (mxGPUGetClassID(gpsi) != mxSINGLE_CLASS) mexErrMsgIdAndTxt(errId, errMsg);
u0 = (float *)(mxGPUGetData(gu0));
psi = (float *)(mxGPUGetData(gpsi));
/* Create a GPUArray to hold the result and get its underlying pointer. */
gu = mxGPUCreateGPUArray(mxGPUGetNumberOfDimensions(gu0),
mxGPUGetDimensions(gu0),
mxGPUGetClassID(gu0),
mxGPUGetComplexity(gu0),
MX_GPU_DO_NOT_INITIALIZE);
u = (float *)(mxGPUGetData(gu));
// Main function
VSNR_ADMM_GPU(u0,psi,n0,n1,nit,beta,u,dimGrid,dimBlock);
// Wrap the result up as a MATLAB gpuArray for return.
plhs[0] = mxGPUCreateMxArrayOnGPU(gu);
// Destroys the copies
mxGPUDestroyGPUArray(gu0);
mxGPUDestroyGPUArray(gpsi);
}