小编Fra*_*ter的帖子

(int(*)[])var1代表什么?

我找到了这个示例代码,我试图google什么(int (*)[])var1可以代表,但我没有得到有用的结果.

#include <unistd.h>
#include <stdlib.h>

int i(int n,int m,int var1[n][m]) {
    return var1[0][0];
}

int example() {
    int *var1 = malloc(100);
    return i(10,10,(int (*)[])var1);
} 
Run Code Online (Sandbox Code Playgroud)

通常我在C99中使用VLA,所以我习惯于:

#include <unistd.h>
#include <stdlib.h>

int i(int n,int m,int var1[n][m]) {
    return var1[0][0];
}

int example() {
    int var1[10][10];
    return i(10,10,var1);
} 
Run Code Online (Sandbox Code Playgroud)

谢谢!

c c99 variable-length multidimensional-array

8
推荐指数
1
解决办法
563
查看次数

如何为Matlab提供它想要的旧gcc版本?

在我的电脑上,我有Ubuntu 10.10和gcc 4.4.4.我尝试编译一些使用CUDA的mex文件,我收到以下错误消息:

>> cns_build('hmax')
compiling...
/home/leMe/hmax/cns/source/common_dec.h(54): warning: omission of exception specification is incompatible with previous function "operator new(size_t)"
/usr/include/c++/4.4/new(91): here
/home/leMe/hmax/cns/source/common_dec.h(55): warning: omission of exception specification is incompatible with previous function "operator new[](size_t)"
/usr/include/c++/4.4/new(92): here
/home/leMe/hmax/cns/source/common_dec.h(56): warning: omission of exception specification is incompatible with previous function "operator delete(void *)"
/usr/include/c++/4.4/new(93): here
/home/leMe/hmax/cns/source/common_dec.h(57): warning: omission of exception specification is incompatible with previous function "operator delete[](void *)"
/usr/include/c++/4.4/new(94): here

Segmentation fault
CUDA preprocessing [nvcc] failed
Warning: You are using gcc version …
Run Code Online (Sandbox Code Playgroud)

matlab gcc cuda mex

8
推荐指数
1
解决办法
9133
查看次数

尽管存在memset,但"使用未初始化的值"

我分配了一个2d数组并使用memset用零填充它.

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

void main() {
    int m=10;
    int n =10;
    int **array_2d;
    array_2d = (int**) malloc(m*sizeof(int*));
    if(array_2d==NULL) {
        printf("\n Could not malloc 2d array \n");
        exit(1);
    }
    for(int i=0;i<m;i++) {
        ((array_2d)[i])=malloc(n*sizeof(int));
        memset(((array_2d)[i]),0,sizeof(n*sizeof(int)));
    }


    for(int i=0; i<10;i++){
        for(int j=0; j<10;j++){
            printf("(%i,%i)=",i,j);
            fflush(stdout);
            printf("%i ", array_2d[i][j]);
        }
        printf("\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

之后我使用valgrind [1]来检查内存错误.我得到以下错误:Conditional jump or move depends on uninitialised value(s)第24行(printf("%i ", array_2d[i][j]);).我一直认为memset是初始化数组的函数.我怎样才能摆脱这个错误?

谢谢!

Valgrind输出:

==3485== Memcheck, a memory error detector
==3485== Copyright (C) 2002-2009, and GNU GPL'd, …
Run Code Online (Sandbox Code Playgroud)

c malloc valgrind memset

7
推荐指数
1
解决办法
2893
查看次数

我应该调查PTX来优化我的内核吗?如果是这样,怎么样?

您是否建议阅读内核的PTX代码以进一步优化内核?

一个例子:我读过,如果自动循环展开有效,可以从PTX代码中找到.如果不是这种情况,则必须在内核代码中手动展开循环.

  • PTX代码还有其他用例吗?
  • 你看看你的PTX代码了吗?
  • 在哪里可以找到如何读取CUDA为我的内核生成的PTX代码?

performance cuda gpgpu loop-unrolling ptx

7
推荐指数
1
解决办法
1771
查看次数

与布尔numpy数组VS PEP8 E712的比较

PEP8 E712要求"比较True应该if cond is True:if cond:".

但如果我遵循这一点,PEP8我会得到不同/错误的结果.为什么?

In [1]: from pylab import *

In [2]: a = array([True, True, False])

In [3]: where(a == True)
Out[3]: (array([0, 1]),)
# correct results with PEP violation

In [4]: where(a is True)
Out[4]: (array([], dtype=int64),)
# wrong results without PEP violation

In [5]: where(a)
Out[5]: (array([0, 1]),)
# correct results without PEP violation, but not as clear as the first two imho. "Where what?"
Run Code Online (Sandbox Code Playgroud)

python numpy pep8

7
推荐指数
2
解决办法
2448
查看次数

如何声明extern typedef结构?

我有两个c文件,foo.c的功能和test_foo.c,它们测试foo.c的功能.

有没有办法BAR在不使用头文件的情况下访问test_foo.c中foo.c中定义的struct typedef ?到目前为止,我能够避免使用啊文件,因此整个程序将由foo.c组成.谢谢.

foo.c   
typedef struct BAR_{...} bar;
BAR *bar_new(...) {..}

test_foo.c
extern BAR *bar_new(...)
Run Code Online (Sandbox Code Playgroud)

error: expected declaration specifiers or ‘...’ before ‘BAR’

c extern

6
推荐指数
1
解决办法
3万
查看次数

有条件的"pragma omp"

我正在使用OpenMP尝试不同类型的并行化.结果我#pragma omp parallel for在我的代码中有几行我(un-)交替评论.有没有办法让这些行条件如下所示,而不是工作代码?

   define OMPflag 1 
   #if OMPFlag pragma omp parallel for
   for ...
Run Code Online (Sandbox Code Playgroud)

c macros pragma openmp

6
推荐指数
2
解决办法
4455
查看次数

PyCUDA:查询设备状态(具体是内存)

PyCUDA的文档传递了驱动程序接口调用,但我有点想,并且看不出如何从我的代码中获取诸如'SHARED_SIZE_BYTES'之类的信息.

有人能指出我以这种方式查询设备的任何例子吗?

是否可以/如何检查设备状态(例如,在malloc/memcpy和内核启动之间)以实现某些机器动态操作?(我希望能够以"友好"的方式处理支持多个内核的设备.

python cuda gpgpu device-driver pycuda

6
推荐指数
1
解决办法
4555
查看次数

如何测量矩阵乘法内核的gflops?

在书中Programming Massively Parallel Processors,gflops的数量用于比较不同矩阵乘法核的效率.我如何在自己的机器上为自己的内核计算这个?

在NVIDIA论坛的某个地方,我找到了这个"算法",但我不知道它是多么有效或两次来自何处.

NumOps = 2 * pow(MatrixSize,3)
gflops = 1.0e-9 * NumOps / ExecutionTime
Run Code Online (Sandbox Code Playgroud)

ps请随意更改标签...

benchmarking cuda gpgpu

6
推荐指数
1
解决办法
3791
查看次数

使用gevent下载图像

我的任务是从给定的网址列表中下载1M +图像.建议的方法是什么?

阅读Greenlet Vs. 我调查过的线程gevent,但是我无法可靠地运行它.我玩了100个网址的测试集,有时它在1.5秒内完成,但有时它需要超过30秒,这很奇怪,因为每个请求的超时*为0.1,所以它永远不会超过10秒.

*见下面的代码

我也调查过,grequests但他们似乎有异常处理问题.

我的"要求"就是我能做到的

  • 检查下载时出现的错误(超时,损坏的图像......),
  • 监控已处理图像的数量和进度
  • 尽可能快.
from gevent import monkey; monkey.patch_all()
from time import time
import requests
from PIL import Image
import cStringIO
import gevent.hub
POOL_SIZE = 300


def download_image_wrapper(task):
    return download_image(task[0], task[1])

def download_image(image_url, download_path):
    raw_binary_request = requests.get(image_url, timeout=0.1).content
    image = Image.open(cStringIO.StringIO(raw_binary_request))
    image.save(download_path)

def download_images_gevent_spawn(list_of_image_urls, base_folder):
    download_paths = ['/'.join([base_folder, url.split('/')[-1]])
                      for url in list_of_image_urls]
    parameters = [[image_url, download_path] for image_url, download_path in
             zip(list_of_image_urls, download_paths)]
    tasks = [gevent.spawn(download_image_wrapper, …
Run Code Online (Sandbox Code Playgroud)

python concurrency gevent greenlets

6
推荐指数
1
解决办法
915
查看次数