# Random Number Function Vectorization

Compiler Methodology for Intel® MIC Architecture

## Vectorization Essentials, Random Number Function Vectorization

The Intel Compiler version 13.0 supports random number auto- vectorization of the drand48 family of random number functions in C/C++ and RANF and Random_Number functions in Fortran. Vectorization is supported through the Intel Short Vector Math Library (SVML).

Supported C/C++ Functions:

```double drand48(void);
double erand48(unsigned short xsubi[3]);
long int lrand48(void);
long int nrand48(unsigned short xsubi[3]);
long int mrand48(void);
long int jrand48(unsigned short xsubi[3]);```

### Simple Examples:

#### 1. drand48 vectorization.

```#include <stdlib.h>

#include <stdio.h>
#define ASIZE 1024

int main(int argc, char *argv[])
{
int i;
double rand_number[ASIZE] = {0};
unsigned short seed[3] = {155,0,155};

// Initialize Seed Value For Random Number
seed48(&seed[0]);

for (i = 0; i < ASIZE; i++){
rand_number[i] = drand48();
}

// Sample Array Element
printf("%f\n", rand_number[ASIZE-1]);
return 0;
}```

#### 2. erand48 vectorization, seed value is passed as an argument.

```#include <stdlib.h>

#include <stdio.h>
#define ASIZE 1024

int main(int argc,  char *argv[])
{
int i;
double rand_number [ASIZE] = {0};
unsigned short seed[3] = {155,0,155};

#pragma ivdep
for (i = 0; i < ASIZE; i++){
rand_number[i] = erand48(&seed[0]);
}

// Sample Array Element
printf("%f\n", rand_number[ASIZE-1]);
return 0;
}```

#### 3. lrand48 vectorization

```#include <stdlib.h>
#include <stdio.h>
#define ASIZE 1024

int main(int argc, char *argv[])
{
int i;
long rand_number[ASIZE] = {0};
unsigned short seed[3] = {155,0,155};

// Initialize Value For Random Number
seed48(&seed[0]);

for (i = 0; i < ASIZE; i++){
rand_number[i] = lrand48();
}

// Sample Array Element
printf("%ld\n", rand_number[ASIZE-1]);
return 0;
}```

#### 4. nrand48 vectorization, seed value id passed as an argument.

```#include <stdlib.h>
#include <stdio.h>
#define ASIZE 1024

int main(int argc,  char *argv[])
{
int i;
long rand_number[ASIZE] = {0};
unsigned short seed[3] = {155,0,155};

#pragma ivdep
for (i = 0; i < ASIZE; i++){
rand_number[i] = nrand48(&seed[0]);
}

// Sample Array Element
printf("%ld\n", rand_number[ASIZE-1]);
return 0;
}```

#### 5. mrand48 vectorization.

```#include <stdlib.h>

#include <stdio.h>
#define ASIZE 1024

int main(int argc,  char *argv[])
{
int i;
long rand_number[ASIZE] = {0};
unsigned short seed[3] = {155,0,155};

// Initialize Seed Value For Random Number
seed48(&seed[0]);

for (i = 0; i < ASIZE; i++){
rand_number[i] = mrand48();
}

// Sample Array Element
printf("%ld\n", rand_number[ASIZE-1]);
return 0;
}```

#### 6. jrand48 vectorization, seed value is passed as an argument.

```#include <stdlib.h>
#include <stdio.h>
#define ASIZE 1024

int main(int argc,  char *argv[])
{
int i;
long rand_number[ASIZE] = {0};
unsigned short seed[3] = {155,0,155};

#pragma ivdep
for (i = 0; i < ASIZE; i++){
rand_number[i] = jrand48(&seed[0]);
}

// Sample Array Element
printf("%ld\n", rand_number[ASIZE-1]);
return 0;
}```

### Fortran Support:

For Fortran we support:

```RANF()

RANDOM_NUMBER() single precision

RANDOM_NUMBER() double precision```

NEXT STEPS

It is essential that you read this guide from start to finish using the built-in hyperlinks to guide you along a path to a successful port and tuning of your application(s) on Intel® Xeon Phi™ architecture. The paths provided in this guide reflect the steps necessary to get best possible application performance.

Back the main chapter Vectorization Essentials.

Для получения подробной информации о возможностях оптимизации компилятора обратитесь к нашему Уведомлению об оптимизации.
Категории: