- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
语句的含义是什么
// 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 << y
是x * 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/
for (i = 0; i <= 1000; i++) { if ( i % 3 === 0){ console.log(i); } if ( i % 5 ==
对于一项作业,我需要解决一个数学问题。我将其缩小为以下内容: 令 A[1, ... ,n] 为 n 整数数组。 令y 为整数常量。 现在,我必须编写一个算法,在 O(n) 时间内找到 M(y) 的最小
我可以使用 iOS MediaPlayer 并通过这种方式播放电影。但我需要,寻找一秒钟的电影。我该怎么做,我像这样通过 MediaPlayer 播放电影: NSURL *videoURL =
我听说过 eCos看起来作为一个爱好项目来玩会很有趣。 任何人都可以推荐一个价格合理的开发板。如果它不会增加太多成本,我想要几个按钮来按下(并以编程方式检测按下)和一些调试输出的 LCD。以太网会很好
给定 a 到 b 的范围和数字 k ,找到 a 到 b [包括两者]之间的所有 k-素数。 k-素数的定义:如果一个数恰好有 k 个不同的素数因子,则该数是 k-素数。 即 a=4 , b=10 k=
这是对 my previous question 的重新措辞尝试作为它收到的反馈的结果。 我想要一个简单的网络通信,我可以将其用作底层框架,而无需再次查看。我只想将一个字符串从一台 PC 推送到另一台
我有许多节点通过其他类型的中间节点连接。如图所示,中间节点可以有多个。我需要找到给定数量的节点的所有中间节点,并按初始节点之间的链接数量对其进行排序。在我的示例中,给定 A、B、C、D,它应该返回节点
我的代码遇到问题。我试图找到这个 5x5 数组的总和,但它总是给我总计 0。当我使用 2x2 数组时,它可以工作,但对于 5x5 数组则不起作用。有人可以帮忙吗? import java.util.*
我们有一个给定的数组,我们想要打印 BST 中每个节点的级别。 例如,如果给定数组为:{15, 6, 2, 10, 9, 7, 13} 那么答案是: 1 2 3 3 4 5 4 (表示存储15的节点级
我对 R 和编程非常陌生,所以请留在我身边:) 我正在尝试使用迭代来查找无限迭代到小数点后第四位的值。 IE。其中小数点后第四位不变。所以 1.4223,其中 3 不再改变,所以小数点后 3 位的结果
我的问题与 Fastest way of computing the power that a "power of 2" number used? 非常相似: 将 x=2^y 作为输入,我想输出 y。
如何找到三个非零数字中最小的一个。 我尝试引入一个非常小的数字eps = 1e-6(我的数字为零或明显大于eps)并在min(x,eps)、min(y,eps)之间进行测试)等我什么也没得到。有没有办
我有一个类(class),他们计算矩阵中最大的“1”岛,但他的岛概念是“如果两个单元在水平、垂直或对角线上彼此相邻,则称它们是相连的。 “ 我需要帮助来删除对角台阶。 class GFG {
我开始使用 IDE Jupyter && Python 3.6 并出现了一个问题。我必须通过IDE绘制Petersen子图中的哈密顿路径,但我不知道该怎么做。 我显示有关该图的信息: Petersen
public static void main(String[] args) { int sum = 2; int isPrime; for(int x = 3; x Mat
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How much time should it take to find the sum of all prime
我想找到给定节点到链表二叉搜索树中根的距离。我有下面的代码来计算树的高度(root.getHeightN()),从根到叶子,但我现在需要的是从叶子到根。 public int getHeightN()
是否有一种优雅的方法使用预先计算的 KDTree 来查找连接组件的数量?现在使用呼吸优先搜索算法以及 k 最近邻的 KDTree 给出的邻接矩阵来查找连接的组件,但是是否有更好的可能性? import
我有一个要求,我需要找到具有相同名称的不同对象中 amt 值的总和。下面是代码片段 traveler = [ { description: 'Senior', Amount: 50}, {
我正在尝试使用 pandas 对某些列进行求和,同时保留其他列。例如: member_no, data_1, data_2, data_3, dat_1, dat_2, other_1, other_
我是一名优秀的程序员,十分优秀!