-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
67 lines (49 loc) · 1.42 KB
/
common.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
/* common.c
Contains functions Malloc and calcNumsPerProcess.
Malloc allocates memory through malloc and aborts MPI, if malloc
fails
calcNumsPerProcess calculates how many "numbers" to be
allocated to a process.
*/
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
#include "mpi.h"
void * Malloc(int num_elements, int el_size, MPI_Comm comm, int rank)
{
void *b = malloc(num_elements*el_size);
if (b==NULL)
{
fprintf(stderr, "*** PROCESS %d: MALLOC COULD NOT ALLOCATE %d ELEMENTS OF SIZE %d BYTES\n",
rank, num_elements, el_size);
MPI_Abort( comm, 1);
}
return b;
}
void nextPrime(mpz_t current_prime, mpz_t previous){
int is_prime = 0;
int reps = 25;
while(is_prime == 0){
mpz_nextprime(current_prime, previous);
is_prime = mpz_probab_prime_p(current_prime, reps);
}
}
void calcRangePerProcess(mpz_t N, int p, mpz_t low, mpz_t *nums_per_node)
{
int i;
mpz_t modulo;
mpz_init(modulo);
mpz_init_set(nums_per_node[0], low);
for (i = 1; i < p; i++) {
mpz_init(nums_per_node[i]);
/*nums_per_node[i] += nums_per_node[i] + N/p*/
mpz_add(nums_per_node[i], nums_per_node[i], nums_per_node[i-1]);
mpz_cdiv_q_ui(modulo,N,p);
mpz_add(nums_per_node[i], nums_per_node[i], modulo);
/*modulo = N%p*/
mpz_mod_ui(modulo, N, p);
if(mpz_cmp_ui(modulo, i) < 0){
mpz_add_ui(nums_per_node[i], nums_per_node[i], 1);
}
}
}