-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat_vect.c
163 lines (121 loc) · 3.53 KB
/
mat_vect.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
#include "cmsis.h"
#include "gap_common.h"
#include "mbed_wait_api.h"
#include <stdio.h>
// FEATURE_CLUSTER
#include "gap_cluster.h"
#include "gap_dmamchan.h"
#include <stdlib.h>
#include <time.h>
#define FC_FREQ (300000000)
#define CLUSTER_FREQ (200000000)
#define F_DIV (1000000)
#define NPOINTS (10000000)
#define NUM_THREADS (8)
#define CORE_NUMBER (8)
uint32_t current_voltage(void)
{
return DCDC_TO_mV(PMU_State.DCDC_Settings[READ_PMU_REGULATOR_STATE(PMU_State.State)]);
}
int thread_count;
int m, n;
double* A;
double* x;
double* y;
/* Serial functions */
void Usage(char* prog_name);
void Read_matrix(char* prompt, double A[], int m, int n);
void Read_vector(char* prompt, double x[], int n);
void Print_matrix(char* title, double A[], int m, int n);
void Print_vector(char* title, double y[], double m);
/* Parallel function */
void *mat_vect(void* rank);
void Master_Entry(int * L1_mem)
{
CLUSTER_CoresFork(mat_vect, (void *) L1_mem);
}
int main(int argc, char argv[])
{
long thread;
int* thread_handles;
//if (argc != 2) Usage(argv[0]);
thread_count =atoi(argv[1]);
thread_handles = malloc(thread_count*sizeof(int));
//int m, n;
m = argv[2];
n = argv[3];
A = malloc(m*n*sizeof(double));
x = malloc(n*sizeof(double));
y = malloc(m*sizeof(double));
Read_matrix("Enter the matrix", A, m, n);
Print_matrix("We read", A, m, n);
Read_vector("Enter the vector", x, n);
Print_vector("We read", x, n);
int *L1_mem = L1_Malloc(8);
FLL_SetFrequency(uFLL_SOC, FC_FREQ, 0);
CLUSTER_Start(0,CORE_NUMBER);
if (FLL_SetFrequency(uFLL_CLUSTER, CLUSTER_FREQ, 0) == -1)
{
printf("Error of changing frequency, check Voltage value!\n");
}
printf("FC Frequency: %d MHz - Cluster Frequency: %d MHz - Voltage: %lu mV\n",
FLL_GetFrequency(uFLL_SOC)/F_DIV, FLL_GetFrequency(uFLL_CLUSTER)/F_DIV, current_voltage());
CLUSTER_SendTask(0, Master_Entry, (void *) thread_handles[thread], 0);
printf("Waiting...\n");
CLUSTER_Wait(0);
/* for (thread = 0; thread < thread_count; thread++)
pthread_create(&thread_handles[thread], NULL,
Pth_mat_vect, (void*) thread);
*/
CLUSTER_Wait(0);
Print_vector("The product is", y, m);
// for (int i = 0; i < 8; i++) {
// count += L1_mem[i];
// printf("CORE %d - count = %d\n", i, L1_mem[i]);
// }
/* Cluster Stop - Power down */
CLUSTER_Stop(0);
printf("DONE!\n");
Print_vector("The product is", y, m);
free(A);
free(x);
free(y);
exit(0);
}
/*void Usage (char* prog_name) {
fprintf(stderr, "usage: %s <thread_count>\n", prog_name);
exit(0);
}*/ /* Usage */
void Read_matrix(char* prompt, double A[], int m, int n) {
int i, j;
printf("%s\n", prompt);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%lf", &A[i*n+j]);
} /* Read_matrix */
/*------------------------------------------------------------------
* Function: Read_vector
* Purpose: Read in the vector x
* In arg: prompt, n
* Out arg: x
*/
void Read_vector(char* prompt, double x[], int n) {
int i;
printf("%s\n", prompt);
for (i = 0; i < n; i++)
scanf("%lf", &x[i]);
} /* Read_vector */
void *mat_vect(void*rank)
{
long my_rank = (long) rank;
int i, j;
int local_m = m/CORE_NUMBER;
int my_first_row = my_rank*local_m;
int my_last_row = (my_rank+1)*local_m - 1;
for (i = my_first_row; i <= my_last_row; i++) {
y[i] = 0.0;
for (j = 0; j < n; j++)
y[i] += A[i*n+j]*x[j];
}
return NULL;
}