question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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/?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
robertwbcommented, Nov 9, 2018

Yes, for sure. PRs welcome.

On Fri, Nov 9, 2018, 5:17 AM Leonard Lausen <notifications@github.com wrote:

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_pairT,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/?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/cython/cython/issues/2706, or mute the thread https://github.com/notifications/unsubscribe-auth/AAdqgdEq4NjnD9Gt0IV6ctDBGN0Z3vb4ks5utQHAgaJpZM4YV702 .

0reactions
CrimsonVexcommented, Dec 28, 2019

Fixed by changing make_pair( 1, 2 ) to pair[ int, int ]( 1, 2 )

Read more comments on GitHub >

github_iconTop Results From Across the Web

std::make_pair - cppreference.com
Creates a std::pair object, deducing the target type from the types of arguments. The deduced types V1 and V2 are std::decay<T1> ...
Read more >
std::make_pair - CPlusPlus.com
Constructs a pair object with its first element set to x and its second element set to y . The template types can...
Read more >
make_pair vs the constructor of std::pair? - Stack Overflow
The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the ......
Read more >
std::make_pair - cppreference.com
Creates a std::pair object, deducing the target type from the types of arguments. The deduced types V1 and V2 are std::decay<T1> ...
Read more >
C++ Utility Library - make_pair Function - Tutorialspoint
C++ Utility Library - make_pair Function ; Return Value. It returns a pair object whose elements first and second are set to x...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found