Why does “vectorizing” this simple R loop give a different result?

前端 未结 4 926
故里飘歌
故里飘歌 2021-02-12 21:54

Perhaps a very dumb question.

I am trying to \"vectorize\" the following loop:

set.seed(0)
x <- round(runif(10), 2)
# [1] 0.90 0.27 0.37 0.57 0.91 0.2         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-12 22:19

    Interesting to see that although R "vectorization" is different from "SIMD" (as OP nicely explained), the same logic applies when determining whether a loop is "vectorizable". Here is a demo using examples in OP's self-answer (with a little modification).

    Example 1 with "write-after-read" dependency is "vectorizable".

    // "ex1.c"
    #include 
    void ex1 (size_t n, size_t *x) {
      for (size_t i = 1; i < n; i++) x[i - 1] = x[i] + 1;
    }
    
    gcc -O2 -c -ftree-vectorize -fopt-info-vec ex1.c
    #ex1.c:3:3: note: loop vectorized
    

    Example 2 with "read-after-write" dependency is not "vectorizable".

    // "ex2.c"
    #include 
    void ex2 (size_t n, size_t *x) {
      for (size_t i = 1; i < n; i++) x[i] = x[i - 1] + 1;
    }
    
    gcc -O2 -c -ftree-vectorize -fopt-info-vec-missed ex2.c
    #ex2.c:3:3: note: not vectorized, possible dependence between data-refs
    #ex2.c:3:3: note: bad data dependence
    

    Use C99 restrict keyword to hint compiler of no memory block aliasing between three arrays.

    // "ex3.c"
    #include 
    void ex3 (size_t n, size_t * restrict a, size_t * restrict b, size_t * restrict c) {
      for (size_t i = 0; i < n; i++) a[i] = b[i] + c[i];
    }
    
    gcc -O2 -c -ftree-vectorize -fopt-info-vec ex3.c
    #ex3.c:3:3: note: loop vectorized
    

提交回复
热议问题