Date: 2025-03-29T05:58:42+00:00

Actual source code: ex1.c

      1: static char help[] = "Tests PetscRandom functions.\n\n";

      3: #include <petscsys.h>

      5: /* Usage:
      6:    mpiexec -n <np> ./ex1 -n <num_of_random_numbers> -random_type <type> -log_view
      7:                          -view_randomvalues <view_rank>
      8:                          -random_view ascii -random_view :filename
      9: */

     11: int main(int argc, char **argv)
     12: {
     13:   PetscInt      i, n = 1000, *values;
     14:   PetscRandom   rnd;
     15:   PetscScalar   value, avg = 0.0;
     16:   PetscMPIInt   rank;
     17:   PetscInt      view_rank = -1;
     18:   PetscLogEvent event;

     20:   PetscFunctionBeginUser;
     21:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
     22:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
     23:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
     24:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-view_randomvalues", &view_rank, NULL));

     26:   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rnd));
     27:   /* force imaginary part of random number to always be zero; thus obtain reproducible results with real and complex numbers */
     28:   PetscCall(PetscRandomSetInterval(rnd, 0.0, 1.0));
     29:   PetscCall(PetscRandomSetFromOptions(rnd));

     31:   PetscCall(PetscMalloc1(n, &values));
     32:   for (i = 0; i < n; i++) {
     33:     PetscCall(PetscRandomGetValue(rnd, &value));
     34:     avg += value;
     35:     if (view_rank == rank) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] value[%" PetscInt_FMT "] = %6.4e\n", rank, i, (double)PetscRealPart(value)));
     36:     values[i] = (PetscInt)(n * PetscRealPart(value) + 2.0);
     37:   }
     38:   avg = avg / ((PetscReal)n);
     39:   if (view_rank == rank) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Average value %6.4e\n", rank, (double)PetscRealPart(avg)));

     41:   PetscCall(PetscSortInt(n, values));

     43:   PetscCall(PetscLogEventRegister("Sort", 0, &event));
     44:   PetscCall(PetscLogEventBegin(event, 0, 0, 0, 0));

     46:   PetscCall(PetscRandomSeed(rnd));
     47:   for (i = 0; i < n; i++) {
     48:     PetscCall(PetscRandomGetValue(rnd, &value));
     49:     values[i] = (PetscInt)(n * PetscRealPart(value) + 2.0);
     50:     /* printf("value[%d] = %g\n",i,value); */
     51:   }
     52:   PetscCall(PetscSortInt(n, values));
     53:   PetscCall(PetscLogEventEnd(event, 0, 0, 0, 0));

     55:   for (i = 1; i < n; i++) PetscCheck(values[i] >= values[i - 1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Values not sorted");
     56:   PetscCall(PetscFree(values));
     57:   PetscCall(PetscRandomDestroy(&rnd));

     59:   PetscCall(PetscFinalize());
     60:   return 0;
     61: }

     63: /*TEST

     65:    test:

     67:    test:
     68:       suffix: 2
     69:       nsize: 2
     70:       output_file: output/ex1_1.out

     72:    test:
     73:       suffix: 3
     74:       args: -view_randomvalues 0

     76: TEST*/