-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.cpp
104 lines (90 loc) · 2.08 KB
/
read.cpp
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
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include "read.hpp"
using namespace std;
double func(int k, int n, int i, int j)
{
double ans = 0;
switch(k){
case 1:
ans = n-max(i,j) + 1;
break;
case 2:
ans = max(i,j);
break;
case 3:
ans = fabs(i-j);
break;
case 4:
ans = (double)1/(i+j-1);
break;
}
return ans;
}
void read_func(double* mat, int n, int k)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
mat[n * i + j] = func(k, n, i+1, j+1);
}
}
}
void read_file(double* mat, int n, string name)
{
ifstream in;
in.open(name);
if (!in.is_open())
{
cout << "error opening the file" << endl;
return;
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
in >> mat[i * n + j];
}
}
in.close();
}
void read_func_mpi(double* mat_part, int n, int k,int size,int rank){
int rows;
if(rank + 1 > n%size) rows = n/size;
else rows = n/size + 1;
for (int i = 0; i < rows; i++)
for (int j = 0; j < n; j++)
mat_part[i*n + j] = func(k, n, rank+size*i + 1, j + 1);
}
void read_file_mpi(double* mat_part, int n, string name,int size,int rank){
int rows; // number of rows of matrix_part
if(rank + 1 > n%size) rows = n/size;
else rows = n/size + 1;
ifstream in;
in.open(name);
if (!in.is_open())
{
cout << "error opening the file" << endl;
return;
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < n; j ++)
{
in.ignore(rank + i*size);
in >> mat_part[i*n + j];
}
}
in.close();
}
void E_mat_mpi(double *mat_part,int n,int size,int rank){
int rows;
if(rank + 1 > n%size) rows = n/size;
else rows = n/size + 1;
for (int i = 0; i < rows; i++)
for (int j = 0; j < n; j++)
mat_part[i*n+j] = ( j == rank+size*i) ? 1 : 0;
}