-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.c
173 lines (154 loc) · 4.83 KB
/
utils.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
/***********************************************************************/
/* */
/* The Caml/MPI interface */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1998 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file LICENSE. */
/* */
/***********************************************************************/
/* $Id$ */
/* Utility functions on arrays */
#include <string.h>
#include <mpi.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/bigarray.h>
#include "camlmpi.h"
void caml_mpi_decode_intarray(value data, mlsize_t len)
{
mlsize_t i;
for (i = 0; i < len; i++) Field(data, i) = Long_val(Field(data, i));
}
void caml_mpi_encode_intarray(value data, mlsize_t len)
{
mlsize_t i;
for (i = 0; i < len; i++) Field(data, i) = Val_long(Field(data, i));
}
static value copy_two_doubles(double d0, double d1)
{
value res = caml_alloc_small(2 * Double_wosize, Double_array_tag);
Store_double_field(res, 0, d0);
Store_double_field(res, 1, d1);
return res;
}
value caml_mpi_ba_value(any_ba_value(dv), intnat kind)
{
void *d = dv;
switch (kind) {
default:
CAMLassert(0);
case CAML_BA_FLOAT32:
return caml_copy_double((double) *((float*) d));
case CAML_BA_FLOAT64:
return caml_copy_double(*((double *) d));
case CAML_BA_SINT8:
return Val_int(*((caml_ba_int8 *) d));
case CAML_BA_UINT8:
return Val_int(*((caml_ba_uint8 *) d));
case CAML_BA_SINT16:
return Val_int(*((caml_ba_int16 *) d));
case CAML_BA_UINT16:
return Val_int(*((caml_ba_uint16 *) d));
case CAML_BA_INT32:
return caml_copy_int32(*((int32_t *) d));
case CAML_BA_INT64:
return caml_copy_int64(*((int64_t *) d));
case CAML_BA_NATIVE_INT:
return caml_copy_nativeint(*((intnat *) d));
case CAML_BA_CAML_INT:
return Val_long(*((intnat *) d));
case CAML_BA_COMPLEX32:
{ float * p = (float *) d;
return copy_two_doubles((double) p[0], (double) p[1]); }
case CAML_BA_COMPLEX64:
{ double * p = (double *) d;
return copy_two_doubles(p[0], p[1]); }
case CAML_BA_CHAR:
return Val_int(*((unsigned char *) d));
}
}
void caml_mpi_ba_element(value dv, intnat kind, any_ba_value(rv))
{
void *r = rv;
switch (kind) {
default:
CAMLassert(0);
case CAML_BA_FLOAT32:
*((float *) r) = Double_val(dv); break;
case CAML_BA_FLOAT64:
*((double *) r) = Double_val(dv); break;
case CAML_BA_CHAR:
case CAML_BA_SINT8:
case CAML_BA_UINT8:
*((caml_ba_int8 *) r) = Int_val(dv); break;
case CAML_BA_SINT16:
case CAML_BA_UINT16:
*((caml_ba_int16 *) r) = Int_val(dv); break;
case CAML_BA_INT32:
*((int32_t *) r) = Int32_val(dv); break;
case CAML_BA_INT64:
*((int64_t *) r) = Int64_val(dv); break;
case CAML_BA_NATIVE_INT:
*((intnat *) r) = Nativeint_val(dv); break;
case CAML_BA_CAML_INT:
*((intnat *) r) = Long_val(dv); break;
case CAML_BA_COMPLEX32:
{ float * p = ((float *) r);
p[0] = Double_field(dv, 0);
p[1] = Double_field(dv, 1);
break; }
case CAML_BA_COMPLEX64:
{ double * p = ((double *) r);
p[0] = Double_field(dv, 0);
p[1] = Double_field(dv, 1);
break; }
}
}
#ifdef ARCH_ALIGN_DOUBLE
double * caml_mpi_input_floatarray(value data, mlsize_t len)
{
double * d = caml_stat_alloc(len * sizeof(double));
memcpy(d, (double *) data, len * sizeof(double));
return d;
}
double * caml_mpi_output_floatarray(value data, mlsize_t len)
{
return caml_stat_alloc(len * sizeof(double));
}
void caml_mpi_free_floatarray(double * d)
{
if (d != NULL) caml_stat_free(d);
}
void caml_mpi_commit_floatarray(double * d, value data, mlsize_t len)
{
if (d != NULL) {
memcpy((double *) data, d, len * sizeof(double));
caml_stat_free(d);
}
}
double * caml_mpi_input_floatarray_at_node(value data, mlsize_t len,
value root, value comm)
{
int myrank;
MPI_Comm_rank(Comm_val(comm), &myrank);
if (myrank == Int_val(root))
return caml_mpi_input_floatarray(data, len);
else
return NULL;
}
double * caml_mpi_output_floatarray_at_node(value data, mlsize_t len,
value root, value comm)
{
int myrank;
MPI_Comm_rank(Comm_val(comm), &myrank);
if (myrank == Int_val(root))
return caml_mpi_output_floatarray(data, len);
else
return NULL;
}
#endif