我的老板告诉我为我写的小c文件(foo.c)编写单元测试.我在网上看了很多关于单元测试的背景知识,比如只测试一个函数并使测试完全自动化,但我没有找到任何关于如何实现实际单元测试的教程.我尝试了以下方法,但失败了.
/*foo.c*/
#include foo.h
#if UNIT_TESTING
#define main example_main
#endif
int foo1(...){...}
int foo2(...){...}
int main(int argc,char **argv) {
foo1(...);
foo2(...);
}
/*test_foo.c*/
#include "foo.h"
void main(int argc,char **argv) {
int i = example_main(argc,argv);
return;
}
/*foo.h*/
int example_main(int argc,char **argv);
Run Code Online (Sandbox Code Playgroud)
作为cmd,我使用:
gcc -Wall -pedantic foo.c test_foo.c -DUNIT_TEST=1 -o test_foo.out
Run Code Online (Sandbox Code Playgroud)
我得到以下错误:
test_foo.c: warning: return type of ‘main’ is not ‘int’
test_foo.c: In function ‘main’:
test_foo.c warning: unused variable ‘i’
/tmp/ccbnW95J.o: In function `main':
test_foo.c: multiple definition of `main'
/tmp/ccIeuSor.o:foo.c:(.text+0x538b): first …
Run Code Online (Sandbox Code Playgroud) 我有以下使用位移位的 C for 循环,我想在 python 中重新实现。
n = 64
for(int stride = n>>1; stride >0; stride >>=1)
{...
Run Code Online (Sandbox Code Playgroud)
那么哇,这个循环在 python 中看起来会怎么样?
我知道n>>1
代表除以 2,但我发现很难用 来建模range()
。
我有问题将正确的参数传递给prepare
函数(和到prepared_call)以在PyCUDA中分配共享内存.我以这种方式理解错误消息,我传递给PyCUDA的一个变量是一个long
而不是我想要的float32
.但我看不出变量来自哪里.
此外,在我看来,官方的例子和文件prepare
相互矛盾,是否block
需要None
.
from pycuda import driver, compiler, gpuarray, tools
import pycuda.autoinit
import numpy as np
kernel_code ="""
__device__ void loadVector(float *target, float* source, int dimensions )
{
for( int i = 0; i < dimensions; i++ ) target[i] = source[i];
}
__global__ void kernel(float* data, int dimensions, float* debug)
{
extern __shared__ float mean[];
if(threadIdx.x == 0) loadVector( mean, &data[0], dimensions ); …
Run Code Online (Sandbox Code Playgroud) 我注意到,如果使用argparse参数,bash选项卡完成返回的文件较少.我该如何更改/控制?
最小的示例代码
me@here:~/test$ cat argparsetest.py
import argparse
parser.add_argument('-i', help='input', required=True)
Run Code Online (Sandbox Code Playgroud)
bash完成示例:
# shows all the files
me@here:~/test$ python argparsetest.py
argparsetest.py result.png u1.py
# does not show the image result.png I am actually interested in
me@here:~/test$ python argparsetest.py -i
argparsetest.py u1.py
Run Code Online (Sandbox Code Playgroud)
已经有两个类似的问题,但我没有发现它们有用.
我想知道:这两个项目基本上有相同的目标吗——加快 Python 中的数值计算速度?
有哪些相同点和不同点?
我知道 Theano 的目标并不像 NumPyPy 那样重新实现所有 NumPy,但从我读到的内容来看,Theano 已经可以带来一些非常令人印象深刻的加速结果。那么,如果我们可以为 Theano 编写运行速度快的代码,为什么还需要 NumPyPy呢?
我的训练功能:
def fit(self, X, y):
batch_size = 20
index = T.lscalar() # index to a [mini]batch
updates = {}
return theano.function(
inputs=[index], outputs=self.cost, updates=updates,
givens={
self.sym_X: X[index * batch_size:(index + 1) * batch_size],
self.sym_y: y[index * batch_size:(index + 1) * batch_size]})
Run Code Online (Sandbox Code Playgroud)
然后从其他地方:
fn = obj.fit(X, y)
for i in range(10):
fn(i)
Run Code Online (Sandbox Code Playgroud)
所以我想要的是这样的
fn = obj.fit(X, y)
fn()
Run Code Online (Sandbox Code Playgroud)
我真的不确定如何开始这个,因为theano对我来说仍然非常令人折服.我能够做到这一点,但循环是非常具有挑战性的.
我有一个模糊的概念,如果我可以将theano.function转换为theano.scan,然后在它周围放置一个外部theano.function - 这可能会有效.然而,theano.scan对我来说仍然是神奇的(尽管我付出了最大的努力).
我怎样才能使循环的微型计算机合并到一个函数调用中?
更新:
我以为我拥有它!我懂了:
def fit(self, X, y):
batch_size = 20
n_batches = 5
index = theano.shared(0)
## index to …
Run Code Online (Sandbox Code Playgroud) 对于我正在编写的教程,我正在寻找一个"现实的"简单的例子,说明因无知SIMT/SIMD而造成的死锁.
我想出了这个片段,这似乎是一个很好的例子.
任何输入将不胜感激.
…
int x = threadID / 2;
if (threadID > x) {
value[threadID] = 42;
barrier();
}
else {
value2[threadID/2] = 13
barrier();
}
result = value[threadID/2] + value2[threadID/2];
Run Code Online (Sandbox Code Playgroud)
我知道,既不是CUDA C也不是OpenCL C.
阅读这篇文章F#Versus数学:第一部分 - BLAS和LAPACK入门我偶然发现stack imbalance
了段落中的术语A Warning, Perhaps an Omen
.
我用Google搜索和搜索上的SO,但只能找到人栈的不平衡和不generall解释挣扎.
奖金问题: 它只影响f#还是C,C++,Python,Java等中的一般问题?
如有必要,请更改问题的标签
我正在尝试在Enthought Python Distribution(EPD)上安装Theano,但我得到了一个奇怪的错误.这是我的安装的样子:
C:\Python27
.pip
了使用easy_install pip
pip install Theano
为了测试,我开始ipython
并输入import theano
.我收到以下错误:
Problem occurred during compilation with the command line below:
g++ -shared -g -IC:\Python27\lib\site-packages\numpy\core\include -IC:\Python27\include -o C:\Users\Ove\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_37_Stepping_5_GenuineIntel-2.7.2\lazylinker_ext\lazylinker_ext.pyd C:\Users\Ove\AppData\Local\Theano\compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_37_Stepping_5_GenuineIntel-2.7.2\lazylinker_ext\mod.cpp -LC:\Python27\libs -LC:\Python27 -lpython27
C:\Users\Ove\AppData\Local\Temp\ccIoNPlU.o: In function `initlazylinker_ext':C:/Users/Ove/AppData/Local/Theano/compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_37_Stepping_5_GenuineIntel-2.7.2/lazylinker_ext/mod.cpp:911: undefined reference to `__imp_Py_InitModule4'
collect2: ld returned 1 exit status
Exception: Compilation failed (return status=1): C:\Users\Ove\AppData\Local\Temp. C:/Users/Ove/AppData/Local/Theano/compiledir_Windows-7-6.1.7601-SP1-Intel64_Family_6_Model_37_Stepping_5_GenuineIntel-2.7.2/lazylinker_ext/mod.cpp:911: undefi. collect2: ld returned 1 exit status4'
Run Code Online (Sandbox Code Playgroud)有谁知道如何让Theano与EPD合作?
我想计算sumproduct
Theano中的两个数组.两个数组都被声明为共享变量,并且是先前计算的结果.阅读教程,我发现如何使用扫描来计算我想要的"正常"张量数组,但是当我尝试将代码调整到共享数组时,我收到了错误消息TypeError: function() takes at least 1 argument (1 given)
.(参见下面的最小运行代码示例)
我的代码中的错误在哪里?我的误解在哪里?我也愿意采用不同的方法来解决我的问题.
通常我更喜欢直接获取共享变量的版本,因为在我的理解中,首先将数组转换回Numpy数组,然后再将它们传递给Theano,这将是浪费.
sumproduct
使用共享变量生成代码的错误消息:
import theano
import theano.tensor as T
import numpy
a1 = [1,2,4]
a2 = [3,4,5]
Ta1_shared = theano.shared(numpy.array(a1))
Ta2_shared = theano.shared(numpy.array(a2))
outputs_info = T.as_tensor_variable(numpy.asarray(0, 'float64'))
Tsumprod_result, updates = theano.scan(fn=lambda Ta1_shared, Ta2_shared, prior_value:
prior_value + Ta1_shared * Ta2_shared,
outputs_info=outputs_info,
sequences=[Ta1_shared, Ta2_shared])
Tsumprod_result = Tsumprod_result[-1]
Tsumprod = theano.function(outputs=Tsumprod_result)
print Tsumprod()
Run Code Online (Sandbox Code Playgroud)
错误信息:
TypeError: function() takes at least 1 argument (1 given)
Run Code Online (Sandbox Code Playgroud)
sumproduct
使用 …