gpt4 book ai didi

c - 在 C 或 CUDA 中使用 << 求幂

转载 作者:太空狗 更新时间:2023-10-29 15:59:32 28 4
gpt4 key购买 nike

语句的含义是什么

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

在下面的代码中?它是特定于 CUDA 的还是也可以在标准 C 中使用?

当我 printf 'ed num_elements 我得到 num_elements==1048576

结果是 2^20。那么 << 运算符是 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\n");
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\n", 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 个位置,其效果是将一个数乘以 2 的幂。所以x << yx * 2^y .这是数字在计算机内部存储方式的结果,它是二进制的。

例如,数字1是,当存储为 2 的补码(它是)中的 32 位整数时:

00000000000000000000000000000001

当你做的时候

1 << 20

您正在使用所有 1在那个二进制表示中并将它们移动到 20 上地点:

00000000000100000000000000000000

也就是 2^20。这也适用于符号幅度表示、1 的补码等。

另一个例子,如果你采用 5 的表示:

00000000000000000000000000000101

然后做 5 << 1 , 你得到

00000000000000000000000000001010

这是10 , 或 5 * 2^1 .

相反,>>将通过将位移动到正确 n 个位置来除以 2 的幂。

关于c - 在 C 或 CUDA 中使用 << 求幂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8012602/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com