New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::make_pair #2706

Open
leezu opened this Issue Nov 9, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@leezu

leezu commented Nov 9, 2018

Cython does not include definitions for std::make_pair

The blog post https://molpopgen.github.io/fwdpy/docs/examples/cython/WritingExtensions includes the following example

%%cython --cplus --compile-args=-std=c++11 -I $fwdpy_includes -I $fwdpp_includes -l sequence -l gsl -l gslcblas

from fwdpy.fwdpy cimport *
from libcpp.utility cimport pair
import numpy as np

ctypedef vector[double] vd
ctypedef pair[vd,vd] pvdvd

#Annoyingly, Cython currently does not 
#expose std::make_pair, so we will 
#do it here ourselves!
#If we don't have std::make_pair,
#we end up making extra temporary copies
#of our return values in memory.  That's 
#not cool, as we're doing this because
#we care about efficiency!
cdef extern from "<utility>" namespace "std" nogil:
    pair[T,U] make_pair[T,U](T&,U&)

cdef pvdvd freq_esize_cpp(const singlepop_t * pop):
    cdef vd freq,esize
    cdef double twoN = 2.0*float(pop.N)
    cdef size_t i = 0
    cdef size_t nm=0
    for i in range(pop.mcounts.size()):
        if pop.mcounts[i]>0:
            if pop.mutations[i].neutral is False:
                freq.push_back(float(pop.mcounts[i])/twoN)
                #s records the effect size/selection coefficient
                esize.push_back(pop.mutations[i].s)
    #For some reason, we need to provide casts
    #so that Cython can get the types right for the call to
    #make_pair:
    return make_pair(<vd>freq,<vd>esize)

def freq_esize(Spop pop):
    return freq_esize_cpp(pop.pop.get())

Is this the "best practice" way? If so, would it make sense to include the make_pair definition in Includes/libcpp/?

@robertwb

This comment has been minimized.

Contributor

robertwb commented Nov 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment