File: test_dmatrix.c

package info (click to toggle)
p4est 2.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,536 kB
  • sloc: ansic: 87,528; makefile: 855; sh: 635; perl: 272; python: 226; awk: 40; javascript: 23
file content (346 lines) | stat: -rw-r--r-- 9,011 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
  This file is part of the SC Library.
  The SC Library provides support for parallel scientific applications.

  Copyright (C) 2010 The University of Texas System
  Additional copyright (C) 2011 individual authors

  The SC Library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  The SC Library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with the SC Library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include <sc_dmatrix.h>

#define TEST_DMATRIX_M 4
#define TEST_DMATRIX_N 13

#if defined(SC_WITH_BLAS) && defined(SC_WITH_LAPACK)
static const double eps = 2.220446049250313e-16;
#endif

/**
 * Generates a random number uniformly distributed in `[alpha,beta)`.
 * \return  random number.
 */
static double
test_dmatrix_get_random_uniform (const double alpha, const double beta)
{
  return alpha + (rand () / (RAND_MAX / (beta - alpha)));
}

/**
 * Fills a dmatrix with random numbers.
 */
static void
test_dmatrix_set_random (sc_dmatrix_t * mat, const double alpha,
                         const double beta)
{
  const sc_bint_t     totalsize = mat->m * mat->n;
  double             *mat_data = mat->e[0];
  sc_bint_t           i;

  for (i = 0; i < totalsize; ++i) {
    mat_data[i] = test_dmatrix_get_random_uniform (alpha, beta);
  }
}

/**
 * Checks entries of a matrix comparing to a reference matrix.
 * \return  number of non-identical entries.
 */
static              sc_bint_t
test_dmatrix_check_error_identical (const sc_dmatrix_t * mat_chk,
                                    const sc_dmatrix_t * mat_ref)
{
  const sc_bint_t     totalsize = mat_chk->m * mat_chk->n;
  double             *mat_chk_data = mat_chk->e[0];
  double             *mat_ref_data = mat_ref->e[0];
  sc_bint_t           i;
  sc_bint_t           error_count = 0;

  SC_ASSERT (totalsize == mat_ref->m * mat_ref->n);

  for (i = 0; i < totalsize; ++i) {
    if (1.e4 * DBL_EPSILON < fabs (mat_chk_data[i] - mat_ref_data[i])) {
      error_count++;
    }
  }

  return error_count;
}

#if defined(SC_WITH_BLAS) && defined(SC_WITH_LAPACK)
/**
 * Tests multiplication with matrices of zero number of rows or columns or both.
 */
static void
test_zero_sizes (void)
{
  sc_dmatrix_t       *m1, *m2, *m3;

  m1 = sc_dmatrix_new (0, 3);
  sc_dmatrix_set_value (m1, -5.);

  m2 = sc_dmatrix_clone (m1);
  sc_dmatrix_fabs (m1, m2);
  sc_dmatrix_resize (m2, 3, 0);

  m3 = sc_dmatrix_new (0, 0);
  sc_dmatrix_multiply (SC_NO_TRANS, SC_NO_TRANS, 1., m1, m2, 0., m3);

  sc_dmatrix_destroy (m1);
  sc_dmatrix_destroy (m2);
  sc_dmatrix_destroy (m3);
}
#endif

/**
 * Tests function
 *   sc_dmatrix_scale_shift
 * against
 *   sc_dmatrix_scale -> sc_dmatrix_shift
 *
 * \return  number of entries with errors.
 */
static int
test_scale_shift ()
{
  const double        scale = M_PI;
  const double        shift = M_E;
  sc_dmatrix_t       *mat_chk, *mat_ref;
  sc_bint_t           n_err_entries;

  /* create & fill matrices with random values */
  mat_chk = sc_dmatrix_new (TEST_DMATRIX_M, TEST_DMATRIX_N);
  mat_ref = sc_dmatrix_new (TEST_DMATRIX_M, TEST_DMATRIX_N);
  test_dmatrix_set_random (mat_chk, 0.0, 1.0);
  sc_dmatrix_copy (mat_chk, mat_ref);

  /* compute via function that's being tested */
  sc_dmatrix_scale_shift (scale, shift, mat_chk);

  /* compute reference */
  sc_dmatrix_scale (scale, mat_ref);
  sc_dmatrix_shift (shift, mat_ref);

  /* check error */
  n_err_entries = test_dmatrix_check_error_identical (mat_chk, mat_ref);

  /* destroy */
  sc_dmatrix_destroy (mat_chk);
  sc_dmatrix_destroy (mat_ref);

  /* return number of entries with errors */
  return (int) n_err_entries;
}

/**
 * Tests function
 *   sc_dmatrix_dotmultiply_add
 * against
 *   sc_dmatrix_dotmultiply -> sc_dmatrix_add
 *
 * \return  number of entries with errors.
 */
static int
test_dotmultiply_add ()
{
  sc_bint_t           n_err_entries = 0;

#if defined(SC_WITH_BLAS)
  sc_dmatrix_t       *mat_in, *mat_mult;
  sc_dmatrix_t       *mat_chk, *mat_ref;

  /* create & fill matrices with random values */
  mat_in = sc_dmatrix_new (TEST_DMATRIX_M, TEST_DMATRIX_N);
  mat_mult = sc_dmatrix_new (TEST_DMATRIX_M, TEST_DMATRIX_N);
  mat_chk = sc_dmatrix_new (TEST_DMATRIX_M, TEST_DMATRIX_N);
  mat_ref = sc_dmatrix_new (TEST_DMATRIX_M, TEST_DMATRIX_N);
  test_dmatrix_set_random (mat_in, 0.0, 1.0);
  test_dmatrix_set_random (mat_mult, 0.0, 1.0);
  test_dmatrix_set_random (mat_chk, 0.0, 1.0);
  sc_dmatrix_copy (mat_chk, mat_ref);

  /* compute via function that's being tested */
  sc_dmatrix_dotmultiply_add (mat_mult, mat_in, mat_chk);

  /* compute reference */
  sc_dmatrix_dotmultiply (mat_in, mat_mult);
  sc_dmatrix_add (1.0, mat_mult, mat_ref);

  /* check error */
  n_err_entries = test_dmatrix_check_error_identical (mat_chk, mat_ref);

  /* destroy */
  sc_dmatrix_destroy (mat_in);
  sc_dmatrix_destroy (mat_mult);
  sc_dmatrix_destroy (mat_chk);
  sc_dmatrix_destroy (mat_ref);
#endif

  /* return number of entries with errors */
  return (int) n_err_entries;
}

/**
 * Runs all dmatrix tests.
 */
int
main (int argc, char **argv)
{
  int                 num_failed_tests = 0;
  int                 mpiret, testret;
#if defined(SC_WITH_BLAS) && defined(SC_WITH_LAPACK)
  int                 j;
  sc_dmatrix_t       *A, *x, *xexact, *b, *bT, *xT, *xTexact, *A2, *b2;
  double              xmaxerror = 0.0;
  double              A_data[] = { 8.0, 1.0, 6.0,
    3.0, 5.0, 7.0,
    4.0, 9.0, 2.0
  };
  double              b_data[] = { 1.0, 2.0, 3.0 };
  double              xexact_data[] = { -0.1 / 3.0, 1.4 / 3.0, -0.1 / 3.0 };
#endif

  mpiret = sc_MPI_Init (&argc, &argv);
  SC_CHECK_MPI (mpiret);

  sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_DEFAULT);

#if defined(SC_WITH_BLAS) && defined(SC_WITH_LAPACK)
  A = sc_dmatrix_new_data (3, 3, A_data);
  A2 = sc_dmatrix_clone (A);
  b = sc_dmatrix_new_data (1, 3, b_data);
  b2 = sc_dmatrix_clone (b);
  xexact = sc_dmatrix_new_data (1, 3, xexact_data);
  x = sc_dmatrix_new (1, 3);

  /* Test 1: solve with A from the right */

  sc_dmatrix_rdivide (SC_NO_TRANS, b, A, x);

  sc_dmatrix_add (-1.0, xexact, x);

  xmaxerror = 0.0;
  for (j = 0; j < 3; ++j) {
    xmaxerror = SC_MAX (xmaxerror, fabs (x->e[0][j]));
  }

  SC_LDEBUGF ("xmaxerror = %g\n", xmaxerror);

  if (xmaxerror > 100.0 * eps) {
    ++num_failed_tests;
  }

  /* Test 2: solve with A^T from the left */

  sc_dmatrix_solve_transpose_inplace (A2, b2);

  sc_dmatrix_add (-1.0, xexact, b2);

  xmaxerror = 0.0;
  for (j = 0; j < 3; ++j) {
    xmaxerror = SC_MAX (xmaxerror, fabs (x->e[0][j]));
  }

  SC_LDEBUGF ("xmaxerror = %g\n", xmaxerror);

  if (xmaxerror > 100.0 * eps) {
    ++num_failed_tests;
  }

  /* Test 3: solve with A^T from the right */

  xexact->e[0][0] = 0.05;
  xexact->e[0][1] = 0.3;
  xexact->e[0][2] = 0.05;

  sc_dmatrix_rdivide (SC_TRANS, b, A, x);

  sc_dmatrix_add (-1.0, xexact, x);

  xmaxerror = 0.0;
  for (j = 0; j < 3; ++j) {
    xmaxerror = SC_MAX (xmaxerror, fabs (x->e[0][j]));
  }

  SC_LDEBUGF ("xmaxerror = %g\n", xmaxerror);

  if (xmaxerror > 100.0 * eps) {
    ++num_failed_tests;
  }

  /* Test 4: solve with A from the left */

  bT = sc_dmatrix_new_data (3, 1, b_data);
  xT = sc_dmatrix_new (3, 1);
  xTexact = sc_dmatrix_new (3, 1);

  xTexact->e[0][0] = 0.05;
  xTexact->e[1][0] = 0.3;
  xTexact->e[2][0] = 0.05;

  sc_dmatrix_ldivide (SC_NO_TRANS, A, bT, xT);

  sc_dmatrix_add (-1.0, xTexact, xT);

  xmaxerror = 0.0;
  for (j = 0; j < 3; ++j) {
    xmaxerror = SC_MAX (xmaxerror, fabs (xT->e[0][j]));
  }

  SC_LDEBUGF ("xTmaxerror = %g\n", xmaxerror);

  if (xmaxerror > 100.0 * eps) {
    ++num_failed_tests;
  }

  sc_dmatrix_destroy (xTexact);
  sc_dmatrix_destroy (xT);
  sc_dmatrix_destroy (bT);
  sc_dmatrix_destroy (A);
  sc_dmatrix_destroy (b);
  sc_dmatrix_destroy (x);
  sc_dmatrix_destroy (xexact);
  sc_dmatrix_destroy (A2);
  sc_dmatrix_destroy (b2);

  test_zero_sizes ();
#endif

  /* Test 5: scale & shift */
  testret = test_scale_shift ();
  SC_LDEBUGF ("test_scale_shift: #entries with errors = %i\n", testret);
  if (testret != 0) {
    ++num_failed_tests;
  }

  /* Test 6: dotmultiply & add */
  testret = test_dotmultiply_add ();
  SC_LDEBUGF ("test_dotmultiply_add: #entries with errors = %i\n", testret);
  if (testret != 0) {
    ++num_failed_tests;
  }

  /* finalize sc */
  sc_finalize ();

  /* finalize mpi */
  mpiret = sc_MPI_Finalize ();
  SC_CHECK_MPI (mpiret);

  /* return number of failed tests */
  return num_failed_tests ? EXIT_FAILURE : EXIT_SUCCESS;
}