我正在使用std::futures
并行处理我的算法.我将信息拆分为互斥池,然后在自己的线程中对每个池执行相同的操作.代码如下所示:
class Processor
{
public:
Processor(const std::string &strVal) : m_strVal(strVal)
{
}
std::string GetVal() const {return m_strVal;}
std::vector<std::string> Do()
{
// do some processing - this can throw an exception
}
private:
std::string m_strVal;
};
class ParallelAlgo
{
private:
std::vector<std::string> m_vecMasterResults;
public:
ProcessingFunction(const std::vector<std::string> &vecInfo)
{
// vecInfo holds mutually exclusive pools
std::vector<std::future<std::vector<std::string> > > vecFutures(vecInfo.size());
try
{
for (auto n = 0 ; n < vecInfo.size() ; n++)
{
vecFuture[n] = std::async(std::launch::async, &ParallelAlgo::WorkFunc, vecInfo[n].GetVal());
}
for (auto …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用它std::enable_if
来专门化一个类,如果它的一个子类具有定义的特定成员函数.否则,它应该使用在基类中定义的默认实现.
#include <boost/mpl/list.hpp>
#include <boost/function_types/function_type.hpp>
#include <boost/tti/has_member_function.hpp>
#include <iostream>
#include <type_traits>
#include <memory>
BOOST_TTI_HAS_MEMBER_FUNCTION(f2)
class Base
{
public:
virtual double f1(double x, double y) const
{
std::cout << "Called Base Method" << std::endl;
return 0.0;
}
};
template<typename Derived>
class A : public Base
{
public:
template<typename T = Derived>
typename std::enable_if
< has_member_function_f2< T
, double
, boost::mpl::list<double>
, boost::function_types::const_qualified
>::value
, double
>::type
f1(double x, double y) const
{
std::cout << "Called Derived Method" << std::endl; …
Run Code Online (Sandbox Code Playgroud) 考虑以下计划:
#include <iostream>
int main()
{
int b=3;
int* a=&b;
std::cout<<*a<<'\n';
delete a; // oops disaster at runtime undefined behavior
}
Run Code Online (Sandbox Code Playgroud)
好的,根据C++标准,程序的行为是不确定的.但我的问题是为什么它未被定义?为什么C++的实现不会给出任何编译器错误或任何警告?是否真的很难确定指针的有效性(在编译时检查指针是否由new返回?)是否有任何开销涉及静态确定指针的有效性(即编译时间)?
我以面向对象的风格实现了纯Python代码。在某些方法中存在时间密集型循环,我希望通过对代码进行 cythonizing 来加快速度。
我使用了大量 numpy 数组,并且很难将类转换为 Cython 扩展类型。
这里我声明两个 numpy 数组“verteces”和“norms”作为属性:
将 numpy 导入为 np c将 numpy 导入为 np
cdef class Geometry(object):
cdef:
np.ndarray verteces
np.ndarray norms
def __init__(self, config):
""" Initialization"""
self.config = config
self.verteces = np.empty([1,3,3],dtype=np.float32)
self.norms = np.empty(3,dtype=np.float32)
Run Code Online (Sandbox Code Playgroud)
在运行时,将定义数组的实际大小。调用Geometry.load()
同一个类的方法时会发生这种情况。该方法打开一个 STL 文件并循环遍历三角形条目。
最后我想确定三角形和射线的交点。在相应的方法中,我使用以下声明。
cdef void hit(self, object photon):
""" Ray-triangle intersection according to Moeller and Trumbore algorithm """
cdef:
np.ndarray[DTYPE_t, ndim=3] verteces = self.verteces # nx3x3
np.ndarray[DTYPE_t, ndim=2] norms = self.norms
np.ndarray[DTYPE_t, ndim=1] ph_dir = photon.direction …
Run Code Online (Sandbox Code Playgroud) 我在C中有一些代码需要char缓冲区,如下所示:
char buf[ 64 ];
buf[0] = 0x01;
buf[1] = 0x11;
buf[2] = 0x61;
buf[3] = 0x08;
buf[4] = 0x01;
Run Code Online (Sandbox Code Playgroud)
我试图在Python中创建此缓冲区,并将指针传递给C代码.我创建了如下buf,
buf = create_string_buffer('0x01 0x11 0x61 0x08 0x01', 64)
Run Code Online (Sandbox Code Playgroud)
但是当我将指针传递给我的C代码时,看起来C代码正在读取01的ASCII代码,而不是十六进制文字.
我该如何解决?
我有以下Cython
代码:
from cython import parallel
from libc.stdio cimport printf
def test_func():
cdef int thread_id = -1
with nogil, parallel.parallel(num_threads=10):
thread_id = parallel.threadid()
printf("Thread ID: %d\n", thread_id)
Run Code Online (Sandbox Code Playgroud)
但是,它始终只启动一个线程,即始终只输出
Thread ID: 0
Run Code Online (Sandbox Code Playgroud)
我在做多线程时做错了什么?
我认为我在某处看到用逗号分隔的多于1条指令,
是未定义的行为.
那么以下代码是否会生成未定义的行为?
for (i=0, j=3, k=1; i<3 && j<9 && k<5; i++, j++, k++) {
printf("%d %d %d\n", i, j, k);
}
Run Code Online (Sandbox Code Playgroud)
因为有3条用逗号分隔的指令,
:
i++, j++, k++
Run Code Online (Sandbox Code Playgroud) 我有一个类作为指向抽象基类的指针向量的组合.在组合类中,有很多重复的函数用于将成员函数组合在一起,例如
class Base {
public:
virtual double foo1(double x) = 0;
virtual double foo2(double x) = 0;
};
class Combined : public Base {
std::vector< std::shared_ptr<Base> > bases;
public:
double foo1(double x) {
double rv = 0.0;
for( auto& b : bases ) {
rv += b->foo1(x);
}
return rv;
}
double foo2(double x) {
double rv = 0.0;
for( auto& b : bases ) {
rv += b->foo2(x);
}
return rv;
}
};
Run Code Online (Sandbox Code Playgroud)
感觉我应该能够编写一个函数来抽象该模式而不必为每个方法重复它,因此Combined可以用诸如
class Combined : public Base …
Run Code Online (Sandbox Code Playgroud)