This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaplace2d.cu
More file actions
148 lines (122 loc) · 4.97 KB
/
laplace2d.cu
File metadata and controls
148 lines (122 loc) · 4.97 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
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
/* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <cuda.h>
#include <omp.h>
__global__ void stencil(int imax, int jmax, double *Anew, double *A) {
__shared__ double local[16*4];
int i = threadIdx.x + blockIdx.x*blockDim.x; //global
int j = threadIdx.y + blockIdx.y*blockDim.y; //global
int tid = threadIdx.x + threadIdx.y * blockDim.x; // row contiguous array. This is the thread id.
if (i < imax+1 && j < jmax+1 && i>0 && j>0){
Anew[(j)*(imax+2)+i] = 0.25f * ( A[(j)*(imax+2)+i+1] + A[(j)*(imax+2)+i-1]
+ A[(j-1)*(imax+2)+i] + A[(j+1)*(imax+2)+i]);
local[tid] = fabs(Anew[(j)*(imax+2)+i]-A[(j)*(imax+2)+i]);
}
// error = fmax( error, fabs(Anew[(j)*(imax+2)+i]-A[(j)*(imax+2)+i]));
for (int d=(blockDim.x*blockDim.y)/2; d>0; d=d/2){
__syncthreads();
if (tid < d) local[tid] = fmax(local[tid],local[d+tid]);
}
if (tid == 0){
int bid = blockIdx.x + blockIdx.y*gridDim.x; // row contiguous array. This is the block id.
error[bid] = local[0];
}
}
__global__ void copy(int imax, int jmax, double *Anew, double *A) {
int i = threadIdx.x + blockIdx.x*blockDim.x;
int j = threadIdx.y + blockIdx.y*blockDim.y;
if (i < imax+1 && j < jmax+1 && i>0 && j>0)
A[(j)*(imax+2)+i] = Anew[(j)*(imax+2)+i];
}
int main(int argc, char** argv)
{
//Size along y
int jmax = 4094;
//Size along x
int imax = 4094;
int iter_max = 1000;
const double pi = 2.0 * asin(1.0);
const double tol = 1.0e-5;
double error = 1.0;
double *A;
double *Anew;
double *y0;
A = (double *)malloc((imax+2) * (jmax+2) * sizeof(double));
Anew = (double *)malloc((imax+2) * (jmax+2) * sizeof(double));
y0 = (double *)malloc((imax+2) * sizeof(double));
memset(A, 0, (imax+2) * (jmax+2) * sizeof(double));
// set boundary conditions
for (int i = 0; i < imax+2; i++)
A[(0)*(imax+2)+i] = 0.0;
for (int i = 0; i < imax+2; i++)
A[(jmax+1)*(imax+2)+i] = 0.0;
for (int j = 0; j < jmax+2; j++)
{
y0[j] = sin(pi * j / (jmax+1));
A[(j)*(imax+2)+0] = y0[j];
}
for (int j = 0; j < imax+2; j++)
{
y0[j] = sin(pi * j/ (jmax+1));
A[(j)*(imax+2)+imax+1] = y0[j]*exp(-pi);
}
printf("Jacobi relaxation Calculation: %d x %d mesh\n", imax+2, jmax+2);
double t1 = omp_get_wtime();
int iter = 0;
for (int i = 1; i < imax+2; i++)
Anew[(0)*(imax+2)+i] = 0.0;
for (int i = 1; i < imax+2; i++)
Anew[(jmax+1)*(imax+2)+i] = 0.0;
for (int j = 1; j < jmax+2; j++)
Anew[(j)*(imax+2)+0] = y0[j];
for (int j = 1; j < jmax+2; j++)
Anew[(j)*(imax+2)+jmax+1] = y0[j]*expf(-pi);
double *d_A, *d_Anew, *d_error;
size_t size = (imax+2)*(jmax+2)*sizeof(double);
cudaMalloc(&d_A, size);
cudaMalloc(&d_Anew, size);
cudaMemcpy(d_A, A, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_Anew, Anew, size, cudaMemcpyHostToDevice);
dim3 threads(16,4);
dim3 blocks((imax+2-1)/16+1, (jmax+2-1)/4+1);
/*cudaMalloc(&d_error );*/
while ( error > tol && iter < iter_max )
{
error = 0.0;
stencil<<<blocks, threads>>>(imax, jmax, d_Anew, d_A);
copy<<<blocks, threads>>>(imax, jmax, d_Anew, d_A);
if(iter % 100 == 0) printf("%5d, %0.6f\n", iter, error);
iter++;
}
cudaMemcpy(A, d_A, size, cudaMemcpyDeviceToHost);
double runtime = omp_get_wtime()-t1;
printf(" total: %f s\n", runtime);
}