小编Sim*_*ons的帖子

C++期货并行处理

我正在使用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)

c++ multithreading

13
推荐指数
1
解决办法
777
查看次数

使用std :: enable_if时的对象切片

我正在尝试使用它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)

c++ virtual-functions enable-if object-slicing c++11

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

为什么在C++中释放未定义的无效指针?

考虑以下计划:

#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返回?)是否有任何开销涉及静态确定指针的有效性(即编译时间)?

c++ pointers undefined-behavior delete-operator

4
推荐指数
3
解决办法
252
查看次数

Cython:维度不是“tagPyArrayObject”的成员

我以面向对象的风格实现了纯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)

cython

4
推荐指数
1
解决办法
787
查看次数

如何在python字符串中表示十六进制值?

我在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代码,而不是十六进制文字.

我该如何解决?

c python ctype

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

Cython中的并行性不起作用

我有以下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)

我在做多线程时做错了什么?

python openmp cython

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

用逗号`,`未定义的行为写出3条指令?

我认为我在某处看到用逗号分隔的多于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)

c comma-operator undefined-behavior

2
推荐指数
1
解决办法
142
查看次数

抽象调用以组合类向量的结果

我有一个类作为指向抽象基类的指针向量的组合.在组合类中,有很多重复的函数用于将成员函数组合在一起,例如

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)

c++ c++11

0
推荐指数
1
解决办法
125
查看次数