English 中文(简体)
使用<<>C或CUDA
原标题:Usage of << for exponentiation in C or CUDA

声明的含义是什么

// create arrays of 1M elements
const int num_elements = 1<<20;

在以下法典中: 是否具体针对《世界人权宣言》或《标准C》是否也可使用?

缩略语 I got num_elements=1048576

这一数字为2^20。 因此,C的“带”和“带”;C的操作者是短暂的花招?

// This example demonstrates parallel floating point vector
// addition with a simple __global__ function.

#include <stdlib.h>
#include <stdio.h>


// this kernel computes the vector sum c = a + b
// each thread performs one pair-wise addition
__global__ void vector_add(const float *a,
                           const float *b,
                           float *c,
                           const size_t n)
{
  // compute the global element index this thread should process
  unsigned int i = threadIdx.x + blockDim.x * blockIdx.x;

  // avoid accessing out of bounds elements
  if(i < n)
  {
    // sum elements
    c[i] = a[i] + b[i];
  }
}


int main(void)
{
  // create arrays of 1M elements
  const int num_elements = 1<<20;

  // compute the size of the arrays in bytes
  const int num_bytes = num_elements * sizeof(float);

  // points to host & device arrays
  float *device_array_a = 0;
  float *device_array_b = 0;
  float *device_array_c = 0;
  float *host_array_a   = 0;
  float *host_array_b   = 0;
  float *host_array_c   = 0;

  // malloc the host arrays
  host_array_a = (float*)malloc(num_bytes);
  host_array_b = (float*)malloc(num_bytes);
  host_array_c = (float*)malloc(num_bytes);

  // cudaMalloc the device arrays
  cudaMalloc((void**)&device_array_a, num_bytes);
  cudaMalloc((void**)&device_array_b, num_bytes);
  cudaMalloc((void**)&device_array_c, num_bytes);

  // if any memory allocation failed, report an error message
  if(host_array_a == 0 || host_array_b == 0 || host_array_c == 0 ||
     device_array_a == 0 || device_array_b == 0 || device_array_c == 0)
  {
    printf("couldn t allocate memory
");
    return 1;
  }

  // initialize host_array_a & host_array_b
  for(int i = 0; i < num_elements; ++i)
  {
    // make array a a linear ramp
    host_array_a[i] = (float)i;

    // make array b random
    host_array_b[i] = (float)rand() / RAND_MAX;
  }

  // copy arrays a & b to the device memory space
  cudaMemcpy(device_array_a, host_array_a, num_bytes, cudaMemcpyHostToDevice);
  cudaMemcpy(device_array_b, host_array_b, num_bytes, cudaMemcpyHostToDevice);

  // compute c = a + b on the device
  const size_t block_size = 256;
  size_t grid_size = num_elements / block_size;

  // deal with a possible partial final block
  if(num_elements % block_size) ++grid_size;

  // launch the kernel
  vector_add<<<grid_size, block_size>>>(device_array_a, device_array_b, device_array_c, num_elements);

  // copy the result back to the host memory space
  cudaMemcpy(host_array_c, device_array_c, num_bytes, cudaMemcpyDeviceToHost);

  // print out the first 10 results
  for(int i = 0; i < 10; ++i)
  {
    printf("result %d: %1.1f + %7.1f = %7.1f
", i, host_array_a[i], host_array_b[i], host_array_c[i]);
  }


    // deallocate memory
  free(host_array_a);
  free(host_array_b);
  free(host_array_c);

  cudaFree(device_array_a);
  cudaFree(device_array_b);
  cudaFree(device_array_c);
}
最佳回答

<代码><<的操作者为借方转移操作者。 它使用若干个轨道,例如00101,将其转至左侧n,其效果是乘以两个功率。 www.un.org/spanish/ga/president 因此,数字在内部储存在电脑中,即二元。

举例来说,<代码>1 是,在2个辅助器(即:)中作为32个轨道储存时:

00000000000000000000000000000001

When you do

1 << 20

页: 1

00000000000100000000000000000000

页: 1 这还有助于象征性地代表、1个补充等等。

Another example, if you take the representation of 5:

00000000000000000000000000000101

并且:5 << 1, 您收到

00000000000000000000000000001010

Which is 10, or 5 * 2^1.

Conversely, the >> will divide by a power of 2 by moving the bits over to the right n places.

问题回答

变化不大。 在双轨制中,将20个位置移至左边等于2^20。

edit: 是符合标准C的,也是向用户清楚表明它在20个轨道位置上是单一1个,多于写作的<代码>int a = 1048576;

(标准)C左轮操作员<<通过将左边的比值(二元数字)移至右边的左边的“空间”(右边填满零)所显示的“空间”(右边填充);<20个双面编号,1个,后是20个。 由于双轨制是基数2,每一次向左侧的两倍(基数乘数),即与乘数2。

这种双位数的财产可被利用,以比一般数学功能更快地乘数和分立积极分类。 (同样在小学数学阶段,在行使10..=权力时,可以利用同样数量的财产。)





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签