1 /*----------------------------------------------------------------------------
2  ADOL-C -- Automatic Differentiation by Overloading in C++
3  File:     adalloc.c
4  Revision: $Id: adalloc.c 613 2015-08-11 21:11:54Z kulshres $
5  Contents: C allocation of arrays of doubles in several dimensions
6 
7  Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
8                Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel
9 
10  This file is part of ADOL-C. This software is provided as open source.
11  Any use, reproduction, or distribution of the software constitutes
12  recipient's acceptance of the terms of the accompanying license file.
13 
14 ----------------------------------------------------------------------------*/
15 
16 #include <adolc/adalloc.h>
17 #include "dvlparms.h"
18 
19 #if !defined(ADOLC_NO_MALLOC)
20 #   define ADOLC_CALLOC(n,m) calloc(n,m)
21 #else
22 #   define ADOLC_CALLOC(n,m) rpl_calloc(n,m)
23 #endif
24 #if defined(ADOLC_USE_CALLOC)
25 #  if !defined(ADOLC_NO_MALLOC)
26 #     define ADOLC_MALLOC(n,m) calloc(n,m)
27 #  else
28 #     define ADOLC_MALLOC(n,m) rpl_calloc(n,m)
29 #  endif
30 #else
31 #  if !defined(ADOLC_NO_MALLOC)
32 #     define ADOLC_MALLOC(n,m) malloc(n*m)
33 #  else
34 #     define ADOLC_MALLOC(n,m) rpl_malloc(n*m)
35 #  endif
36 #endif
37 
38 BEGIN_C_DECLS
39 
40 extern void adolc_exit(int errorcode, const char *what, const char* function, const char *file, int line);
41 
42 /****************************************************************************/
43 /*                                              MEMORY MANAGEMENT UTILITIES */
44 /*--------------------------------------------------------------------------*/
populate_dpp(double *** const pointer,char * const memory,int n,int m)45 char* populate_dpp(double ***const pointer, char *const memory,
46                    int n, int m) {
47     char* tmp;
48     double **tmp1; double *tmp2;
49     int i,j;
50     tmp = (char*) memory;
51     tmp1 = (double**)memory;
52     *pointer = tmp1;
53     tmp = (char*)(tmp1+n);
54     tmp2 = (double*)tmp;
55     for (i=0;i<n;i++) {
56         (*pointer)[i] = tmp2;
57         tmp2 += m;
58     }
59     tmp = (char*)tmp2;
60     return tmp;
61 }
62 /*--------------------------------------------------------------------------*/
populate_dppp(double **** const pointer,char * const memory,int n,int m,int p)63 char* populate_dppp(double ****const pointer, char *const memory,
64                            int n, int m, int p) {
65     char* tmp;
66     double ***tmp1; double **tmp2; double *tmp3;
67     int i,j;
68     tmp = (char*) memory;
69     tmp1 = (double***) memory;
70     *pointer = tmp1;
71     tmp = (char*)(tmp1+n);
72     tmp2 = (double**)tmp;
73     for(i=0; i<n; i++) {
74         (*pointer)[i] = tmp2;
75         tmp2 += m;
76     }
77     tmp = (char*)tmp2;
78     tmp3 = (double*)tmp;
79     for(i=0;i<n;i++)
80         for(j=0;j<m;j++) {
81             (*pointer)[i][j] = tmp3;
82             tmp3 += p;
83         }
84     tmp = (char*)tmp3;
85     return tmp;
86 }
87 /*--------------------------------------------------------------------------*/
populate_dppp_nodata(double **** const pointer,char * const memory,int n,int m)88 char* populate_dppp_nodata(double ****const pointer, char *const memory,
89                            int n, int m) {
90 
91     char* tmp;
92     double ***tmp1; double **tmp2;
93     int i,j;
94     tmp = (char*) memory;
95     tmp1 = (double***) memory;
96     *pointer = tmp1;
97     tmp = (char*)(tmp1+n);
98     tmp2 = (double**)tmp;
99     for(i=0; i<n; i++) {
100         (*pointer)[i] = tmp2;
101         tmp2 += m;
102     }
103     tmp = (char*)tmp2;
104     return tmp;
105 }
106 /*--------------------------------------------------------------------------*/
myalloc1(size_t m)107 double* myalloc1(size_t m) {
108     double* A = NULL;
109     if (m>0) {
110       A=(double*)ADOLC_MALLOC(m,sizeof(double));
111       if (A == NULL) {
112         fprintf(DIAG_OUT,"ADOL-C error: myalloc1 cannot allocate %zd bytes\n",
113                 (size_t)(m*sizeof(double)));
114         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
115       }
116     }
117     return A;
118 }
119 
120 /*--------------------------------------------------------------------------*/
myalloc2(size_t m,size_t n)121 double** myalloc2(size_t m, size_t n) {
122     double **A=NULL;
123     if (m>0 && n>0)  {
124       int i;
125       char *Adum = (char*)ADOLC_MALLOC(m*n*sizeof(double)+m*sizeof(double*),sizeof(char));
126       if (Adum == NULL) {
127         fprintf(DIAG_OUT,"ADOL-C error: myalloc2 cannot allocate %zd bytes\n",
128                 (size_t)(m*n*sizeof(double)+m*sizeof(double*)));
129         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
130       }
131       populate_dpp(&A,Adum,m,n);
132     }
133     return A;
134 }
135 
136 /*--------------------------------------------------------------------------*/
myalloc3(size_t m,size_t n,size_t p)137 double*** myalloc3(size_t m, size_t n, size_t p) { /* This function allocates 3-tensors contiguously */
138     double  ***A = NULL;
139     if (m>0 && n>0 && p > 0)  {
140       int i,j;
141       char *Adum = (char*) ADOLC_MALLOC(m*n*p*sizeof(double)+m*n*sizeof(double*)+m*sizeof(double**),sizeof(char));
142       if (Adum == NULL) {
143         fprintf(DIAG_OUT,"ADOL-C error: myalloc3 cannot allocate %zd bytes\n",
144                 (size_t)(m*n*p*sizeof(double)+m*n*sizeof(double*)+m*sizeof(double**)));
145         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
146       }
147       populate_dppp(&A,Adum,m,n,p);
148     }
149     return A;
150 }
151 
152 /*--------------------------------------------------------------------------*/
myfree1(double * A)153 void myfree1(double   *A) {
154     if (A) free((char*) A);
155 }
156 
157 /*--------------------------------------------------------------------------*/
myfree2(double ** A)158 void myfree2(double  **A) {
159     if (A) free((char*) A);
160 }
161 
162 /*--------------------------------------------------------------------------*/
myfree3(double *** A)163 void myfree3(double ***A) {
164     if (A) free((char*) A);
165 }
166 
167 
168 /****************************************************************************/
169 /*                                          SPECIAL IDENTITY REPRESENTATION */
170 
171 /*--------------------------------------------------------------------------*/
myallocI2(int n)172 double   **myallocI2(int n) {
173     double *Idum = (double*)ADOLC_MALLOC((2*n-1),sizeof(double));
174     double   **I = (double**)malloc(n*sizeof(double*));
175     int i;
176     if (Idum == NULL) {
177         fprintf(DIAG_OUT,"ADOL-C error: myallocI2 cannot allocate %i bytes\n",
178                 (int)((2*n-1)*sizeof(double)));
179         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
180     }
181     if (I == NULL) {
182         fprintf(DIAG_OUT,"ADOL-C error: myallocI2 cannot allocate %i bytes\n",
183                 (int)(n*sizeof(double*)));
184         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
185     }
186     Idum += (n - 1);
187     I[0] = Idum;
188     *Idum = 1.0;
189     /* 20020628 olvo n3l: Initialization to 0 */
190     for (i=1; i<n; i++)
191         *(++Idum)= 0.0;
192     Idum-=(n-1);
193     for (i=1; i<n; i++) {
194         I[i] = --Idum;
195         *Idum = 0.0;
196     }
197     return I;
198 }
199 
200 /*--------------------------------------------------------------------------*/
myfreeI2(int n,double ** I)201 void myfreeI2(int n, double** I) {
202     free((char*)I[n-1]);
203     free((char*) I);
204 }
205 
206 /****************************************************************************/
207 /*                              INTEGER VARIANT FOR BIT PATTERN PROPAGATION */
208 
209 /* ------------------------------------------------------------------------- */
myalloc1_uint(int m)210 unsigned int *myalloc1_uint(int m) {
211     unsigned int *A = (unsigned int*)ADOLC_MALLOC(m,sizeof(unsigned int));
212     if (A == NULL) {
213         fprintf(DIAG_OUT, "ADOL-C error, " __FILE__
214                 ":%i : \nmyalloc1_ushort cannot allocate %i bytes\n",
215                 __LINE__, (int)(m*sizeof(unsigned int)));
216         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
217     } /* endif */
218     return A;
219 }
220 
221 
222 /* ------------------------------------------------------------------------- */
myalloc1_ulong(int m)223 unsigned long int *myalloc1_ulong(int m) {
224     unsigned long int *A = (unsigned long int*)ADOLC_CALLOC(m,sizeof(unsigned long int));
225     if (A == NULL) {
226         fprintf(DIAG_OUT, "ADOL-C error, " __FILE__
227                 ":%i : \nmyalloc1_ulong cannot allocate %i bytes\n",
228                 __LINE__, (int)(m*sizeof(unsigned long int)));
229         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
230     } /* endif */
231     return A;
232 }
233 
234 
235 /* ------------------------------------------------------------------------- */
myalloc2_ulong(int m,int n)236 unsigned long int **myalloc2_ulong(int m,int n) {
237     unsigned long int *Adum = (unsigned long int*)ADOLC_CALLOC(m*n,sizeof(unsigned long int));
238     unsigned long int **A   = (unsigned long int**)ADOLC_CALLOC(m,sizeof(unsigned long int*));
239     int i;
240     if (Adum == NULL) {
241         fprintf(DIAG_OUT, "ADOL-C error, " __FILE__
242                 ":%i : \nmyalloc2_ulong cannot allocate %i bytes\n",
243                 __LINE__, (int)(m*n*sizeof(unsigned long int)));
244         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
245     } /* endif */
246     if (A == NULL) {
247         fprintf(DIAG_OUT, "ADOL-C error, " __FILE__
248                 ":%i : \nmyalloc2_ulong cannot allocate %i bytes\n",
249                 __LINE__, (int)(m*sizeof(unsigned long int*)));
250         adolc_exit(-1,"",__func__,__FILE__,__LINE__);
251     } /* endif */
252     for(i=0;i<m;i++) {
253         A[i] = Adum;
254         Adum += n;
255     }
256     return A;
257 
258     /* To deallocate an array set up by   A = myalloc2_ulong(m,n)   */
259     /*    use  free((char*)*A); free((char*)A);  in that order      */
260 
261 }
262 
263 
264 /* ------------------------------------------------------------------------ */
265 
myfree1_uint(unsigned int * A)266 void myfree1_uint(unsigned int *A) {
267     free((char *)A);
268 }
269 
270 /* ------------------------------------------------------------------------ */
271 
myfree1_ulong(unsigned long int * A)272 void myfree1_ulong(unsigned long int *A) {
273     free((char *)A);
274 }
275 
276 /* ------------------------------------------------------------------------ */
277 
myfree2_ulong(unsigned long int ** A)278 void myfree2_ulong(unsigned long int **A) {
279     free((char *)*A);
280     free((char *)A);
281 }
282 
283 
284 END_C_DECLS
285 
286