随机数函数向量化

Drand48 Vectorization in C/C++ Goodman, Steve9700.00000000000

面向英特尔® MIC 架构的编译器方法

向量化要素随机数函数向量化

英特尔 13.0 产品编译器现在支持随机数自动向量化功能C/C++ 中的随机数函数的 drand48 系列和 Fortran 中的 Random_Number 函数。可通过英特尔短向量数学库 (SVML) 支持向量化。

支持的 C/C++ 函数

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]);

简单示例

1. drand48 向量化。

#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. erand38 向量化种子值作为参数传输。

#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. lrand38 向量化。

#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 向量化种子值作为参数传输。

#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 向量化。

#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 向量化种子值作为参数传输。

#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 支持

对于 Fortran 我们支持

RANF()

RANDOM_NUMBER() single precision

RANDOM_NUMBER() double precision

下一步

要在英特尔® 至强 融核架构上成功调试您的应用请务必通读此指南并点击文中的超链接查看相关内容。本指南提供了实现最佳应用性能所要执行的步骤。

返回到主章节“向量化要素? HYPERLINK "http://software.intel.com/en-us/articles/vectorization-essential" 矢量化要素????

 

Para obter informações mais completas sobre otimizações do compilador, consulte nosso aviso de otimização.