title: Vector Sum using AVX Inline Assembly on XeonPhi
body: <p>I am new to use XeonPhi Intel co-processor. I want to write code for a simple Vector sum using AVX 512 bit instructions. I use k1om-mpss-linux-gcc as a compiler and want to write inline assembly. Here it is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;stdint.h&gt;
  
  void* aligned_malloc(size_t size, size_t alignment) {
  
      uintptr_t r = (uintptr_t)malloc(size + --alignment + sizeof(uintptr_t));
      uintptr_t t = r + sizeof(uintptr_t);
      uintptr_t o =(t + alignment) &amp; ~(uintptr_t)alignment;
      if (!r) return NULL;
      ((uintptr_t*)o)[-1] = r;
      return (void*)o;
  }
  
  int main(int argc, char* argv[])
  {
      printf("Starting calculation...\n");
      int i;
      const int length = 65536;
  
      unsigned *A = (unsigned*) aligned_malloc(length * sizeof(unsigned), 64);
      unsigned *B = (unsigned*) aligned_malloc(length * sizeof(unsigned), 64);
      unsigned *C = (unsigned*) aligned_malloc(length * sizeof(unsigned), 64);
  
      for(i=0; i&lt;length; i++){
              A[i] = 1;
              B[i] = 2;
      }
  
      const int AVXLength = length / 16;
      unsigned char * pA = (unsigned char *) A;
      unsigned char * pB = (unsigned char *) B;
      unsigned char * pC = (unsigned char *) C;
      for(i=0; i&lt;AVXLength; i++ ){
              __asm__("vmovdqa32 %1,%%zmm0\n"
                      "vmovdqa32 %2,%%zmm1\n"
                      "vpaddd %0,%%zmm0,%%zmm1;"
              : "=m" (pC) : "m" (pA), "m" (pB));
  
              pA += 64;
              pB += 64;
              pC += 64;
      }
  
      // To prove that the program actually worked
      for (i=0; i &lt;5 ; i++)
      {
              printf("C[%d] = %f\n", i, C[i]);
      }
  
  }
  </code></pre>
  
  <p>However when I run the program, I've got segmentation fault from the asm part. Can somebody help me with that???</p>
  
  <p>Thanks </p>