@@ -8491,3 +8491,73 @@ void PREFIX(sync_team) (caf_team_t *team , int unused __attribute__((unused)))
84918491
84928492 int ierr = MPI_Barrier (* tmp_comm ); chk_err (ierr );
84938493}
8494+
8495+ extern void _gfortran_random_seed_i4 (int32_t * size , gfc_dim1_descriptor_t * put ,
8496+ gfc_dim1_descriptor_t * get );
8497+
8498+ void PREFIX (random_init ) (bool repeatable , bool image_distinct )
8499+ {
8500+ static gfc_dim1_descriptor_t rand_seed ;
8501+ static bool rep_needs_init = true, arr_needs_init = true;
8502+ static int32_t seed_size ;
8503+
8504+ if (arr_needs_init )
8505+ {
8506+ _gfortran_random_seed_i4 (& seed_size , NULL , NULL );
8507+ memset (& rand_seed , 0 , sizeof (gfc_dim1_descriptor_t ));
8508+ rand_seed .base .base_addr = malloc (seed_size * sizeof (int32_t )); // because using seed_i4
8509+ rand_seed .base .offset = -1 ;
8510+ rand_seed .base .dtype .elem_len = sizeof (int32_t );
8511+ rand_seed .base .dtype .rank = 1 ;
8512+ rand_seed .base .dtype .type = BT_INTEGER ;
8513+ rand_seed .base .span = 0 ;
8514+ rand_seed .dim [0 ].lower_bound = 1 ;
8515+ rand_seed .dim [0 ]._ubound = seed_size ;
8516+ rand_seed .dim [0 ]._stride = 1 ;
8517+
8518+ arr_needs_init = false;
8519+ }
8520+
8521+ if (repeatable )
8522+ {
8523+ if (rep_needs_init )
8524+ {
8525+ int32_t lcg_seed = 57911963 ;
8526+ if (image_distinct )
8527+ {
8528+ lcg_seed *= caf_this_image ;
8529+ }
8530+ int32_t * curr = rand_seed .base .base_addr ;
8531+ for (int i = 0 ; i < seed_size ; ++ i )
8532+ {
8533+ const int32_t a = 16087 ;
8534+ const int32_t m = INT32_MAX ;
8535+ const int32_t q = 127773 ;
8536+ const int32_t r = 2836 ;
8537+ lcg_seed = a * (lcg_seed % q ) - r * (lcg_seed / q );
8538+ if (lcg_seed <= 0 ) lcg_seed += m ;
8539+ * curr = lcg_seed ;
8540+ ++ curr ;
8541+ }
8542+ rep_needs_init = false;
8543+ }
8544+ _gfortran_random_seed_i4 (NULL , & rand_seed , NULL );
8545+ }
8546+ else if (image_distinct )
8547+ {
8548+ _gfortran_random_seed_i4 (NULL , NULL , NULL );
8549+ }
8550+ else
8551+ {
8552+ if (caf_this_image == 0 )
8553+ {
8554+ _gfortran_random_seed_i4 (NULL , NULL , & rand_seed );
8555+ MPI_Bcast (rand_seed .base .base_addr , seed_size , MPI_INT32_T , 0 , CAF_COMM_WORLD );
8556+ }
8557+ else
8558+ {
8559+ MPI_Bcast (rand_seed .base .base_addr , seed_size , MPI_INT32_T , 0 , CAF_COMM_WORLD );
8560+ _gfortran_random_seed_i4 (NULL , & rand_seed , NULL );
8561+ }
8562+ }
8563+ }
0 commit comments