-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solver.c
177 lines (167 loc) · 3.72 KB
/
Solver.c
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
#include <stdio.h>
#include "Master.hpp"
#include <stdlib.h>
#include "mpi.h"
#include "dmumps_c.h"
void Solver(int argc, char** argv)
{
MUMPS_INT n;
int lines_irn =0;
int lines_jcn =0;
int lines_rhs =0;
MUMPS_INT8 lines_a =0;
int* irn;
int* jcn;
double* rhs;
double* a;
double time_taken,time_taken_2;
DMUMPS_STRUC_C id;
int myid, ierr;
int error =0;
FILE *file_1 = fopen("irn.txt", "r");
FILE *file_2 = fopen("jcn.txt", "r");
FILE *file_3 = fopen("rhs.txt", "r");
FILE *file_4 = fopen("a.txt", "r");
if(file_1!=NULL && file_2!=NULL && file_3!=NULL && file_4!=NULL)
{
char ch;
while((ch = fgetc(file_1)) != EOF)
{
if(ch == '\n')
{
lines_irn++;
}
}
ch =fgetc(file_2);
while((ch = fgetc(file_2)) != EOF)
{
if(ch == '\n')
{
lines_jcn++;
}
}
ch =fgetc(file_3);
while((ch = fgetc(file_3)) != EOF)
{
if(ch == '\n')
{
lines_rhs++;
}
}
n = lines_rhs;
ch =fgetc(file_4);
while((ch = fgetc(file_4)) != EOF)
{
if(ch == '\n')
{
lines_a++;
}
}
rewind(file_1);
rewind(file_2);
rewind(file_3);
rewind(file_4);
irn = (int*)malloc(lines_irn * sizeof(int));
jcn = (int*)malloc(lines_jcn * sizeof(int));
rhs = (double*)malloc(lines_rhs * sizeof(double));
a = (double*)malloc(lines_a * sizeof(double));
ierr = MPI_Init(&argc, &argv); //MPI initialization
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
if (irn == NULL && jcn == NULL && rhs == NULL && a == NULL)
{
printf("Memory can't be allocated.");
}
else
{
int i=0;
double num;
while(fscanf(file_1, "%lf", &num)!=EOF)
{
irn[i] = num;
i++;
}
i=0;
while(fscanf(file_2, "%lf", &num)!=EOF)
{
jcn[i] = num;
i++;
}
i=0;
while(fscanf(file_3, "%lf", &num)!=EOF)
{
rhs[i] = num;
i++;
}
i=0;
while(fscanf(file_4, "%lf", &num)!=EOF)
{
a[i] = num;
i++;
}
// for(int i =0; i < 50; i++)
// {
// printf("%d\n", rhs[i]);
// }
}
}
else
{
printf("Can't open a file!");
}
fclose(file_1);
fclose(file_2);
fclose(file_3);
fclose(file_4);
time_taken = MPI_Wtime(); //record the start time
id.par =1; id.sym=0;
id.job = -1;
dmumps_c(&id);
if(myid == 0)
{
id.n = n; id.nnz = lines_a; id.irn = irn; id.jcn = jcn;
id.a = a; id.rhs = rhs;
}
#define ICNTL(I) icntl[(I)-1]
id.ICNTL(1) = -1; id.ICNTL(2) = -1; id.ICNTL(3) = -1; id.ICNTL(4) = 0; //Supressing error msgs
//Call the MUMPS package (analyze , factorization and solve)
id.job = 6;
dmumps_c(&id);
if (id.infog[0] < 0)
{
printf("(PROC %d) ERROR RETURN: \tINFOG(1)= %d\n\t\t\t\tINFOG(2)= %d\n",
myid, id.infog[0], id.infog[1]);
error = 1;
}
//Terminate instance
id.job = -2;
dmumps_c(&id);
if (myid == 0)
{
if (!error)
{
FILE *file_sol = fopen("model_prob_sol2.txt", "w");
if(file_sol != NULL)
{
for(int i=0; i < lines_rhs; i++)
{
float d= (float)(i)/(float)(lines_rhs);
fprintf(file_sol, "%f %lf\n",d, rhs[(int)(i)]);
}
fclose(file_sol);
// printf("%d\n",lines_rhs);
printf("Solution is in model_prob_sol2.txt!");
}
else
{
printf("Error opening solution file!");
}
}
else
{
printf("An error occured, please check error code returnd by MUMPS.\n");
}
}
ierr = MPI_Finalize();
time_taken_2 = MPI_Wtime(); //end time
printf("\nTime Taken = %f",time_taken_2 - time_taken);
}