How to declare double const* const* variable in cython? - constants

I have a c++ function in "example.h":
bool myFunc(double const* const* p);
and I want to wrap it with cython code (in .pyx file).
Howerver, when I'm write the following code:
cdef extern from r"example.h":
bool myFunc(double const*const* p)
I'm receiving the following error:
Error compiling Cython file:
Expected ')', found '*'
and pycharm shows this error on double const* const* p:
Unresolved reference 'const'
How can I declare that kind of variables?

In C/C++, there is ongoing battle where to put the const-qualifier: either
void foo(const int *a);
or
void foo(int const *a);
both meaning the same thing.
There is no such battle in Cython, because it accept only the first version.
The above rule, applied to double** leads to:
cdef extern from r"example.h":
bool myFunc(const double * const* p)
Or as a work-around one could drop the const-qualifier altogether:
cdef extern from r"example.h":
bool myFunc(const double **p)
which I would not recommend, all above in large projects, where using const-qualifiers helps a lot when figuring out what happens.

Related

How to use macros to replace/inject function calls in OpenCL

I am developing an algorithm using PyOpenCL. To avoid code duplication I am trying to use templating along with C macros to replace function calls, since OpenCL 1.2 does not support function pointers.
I currently have the following macro section in my OpenCL kernel code:
#define LINEAR_FIT_SEARCH_METHOD ${linear_fit_search_method}
#if LINEAR_FIT_SEARCH_METHOD == MIN_MAX_INTENSITY_SEARCH
#define LINEAR_FIT_SEARCH_METHOD_CALL() determineFitUsingMinMaxIntensitySearch(lineIntensities,imgSizeY,linFitParameter,linFitSearchRangeXvalues)
#elif LINEAR_FIT_SEARCH_METHOD == MAX_INCLINE_SEARCH
#define LINEAR_FIT_SEARCH_METHOD_CALL() determineFitUsingInclineSearch(lineIntensities,imgSizeY,linFitParameter,linFitSearchRangeXvalues,inclineRefinementRange)
#endif
In the kernel code I also define the corresponding functions determineFitUsingMinMaxIntensitySearch and determineFitUsingInclineSearch. I am now attempting to use the macro to exchange the function call like this:
__private struct linearFitResultStruct fitResult = LINEAR_FIT_SEARCH_METHOD_CALL();
so that I select the desired call (note: I always only need either one or the other and configuration is done before the program runs (no need for dynamically switching the two)).
Using PyOpenCL templating I now do something like this:
def applyTemplating(self):
tpl = Template(self.kernelString)
if self.positioningMethod == "maximumIntensityIncline":
linear_fit_search_method="MAX_INCLINE_SEARCH"
if self.positioningMethod == "meanIntensityIntercept":
linear_fit_search_method="MIN_MAX_INTENSITY_SEARCH"
rendered_tpl = tpl.render(linear_fit_search_method=linear_fit_search_method)
self.kernelString=str(rendered_tpl)
Where self.kernelString contains the macro above along with the code.
Unfortunately I am getting this error, which I do not understand:
1:455:53: error: implicit declaration of function 'determineFitUsingInclineSearch' is invalid in OpenCL
1:9:41: note: expanded from macro 'LINEAR_FIT_SEARCH_METHOD_CALL'
1:455:41: error: initializing 'struct linearFitResultStruct' with an expression of incompatible type 'int'
1:536:30: error: conflicting types for 'determineFitUsingInclineSearch'
1:455:53: note: previous implicit declaration is here
1:9:41: note: expanded from macro 'LINEAR_FIT_SEARCH_METHOD_CALL'
1:616:41: error: initializing 'struct linearFitResultStruct' with an expression of incompatible type 'int'
I have very little experience with macros so:
Is what I am attempting even possible in this way or do I need to go a different route?
UPDATE 1:
This code runs fine when I set self.positioningMethod = "meanIntensityIntercept" in my unit test, but fails when setting self.positioningMethod = "maximumIntensityIncline" with the error message above. I cannot spot the error at the yet.
UPDATE 2:
I was also inspired by this post, if that helps:
how to compare string in C conditional preprocessor-directives
As you say you have very little experience with macros then I would go for something simple. determineFitUsingMinMaxIntensitySearch and determineFitUsingInclineSearch accept different number of arguments, so this could done this way:
kernel_code = """
#ifdef USE_FUNCTION_A
void function_a(
int x,
int y,
int extra_param,
__global const int* restrict in,
__global int* restrict out
)
{
//...
}
#else
void function_b(
int x,
int y,
__global const int* restrict in,
__global int* restrict out
)
{
//...
}
#endif
__kernel void my_kernel(
int x,
int y,
__global const int* restrict in,
__global int* restrict out
)
{
// ...
#ifdef USE_FUNCTION_A
function_a(x,y,5,in,out);
#else
function_b(x,y,in,out);
#endif
// ...
}
"""
if use_function_a:
prg = cl.Program(ctx, kernel_code).build("-DUSE_FUNCTION_A")
else:
prg = cl.Program(ctx, kernel_code).build("")

extern struct not recognised in CDT

I have following piece of code in one of my source file under a project in CDT(eclipse).
extern "C" {
void* obj1(int size); /* alloc uninit memory */
void* obj2(int size); /* alloc cleared memory */
void* obj3(void*, int size); /* extend memory, new mem is uninit */
void obj4(void* ptr);
}
I am getting this error message "expected identifier or '(' before string constants"
i think that compiler could not recognise it and i need to supply it with some flag for this purpose.please propose solution for it.It also gives the same message for another piece of code
extern "C" { int NlvStrmatch(const char*, const char*, int);
}

Compiler Warning libpcap

Having followed the examples http://www.tcpdump.org/pcap.htm, and exploring the documentation, I can't see what I have done wrong with the following code
// main.c
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet);
/// ...snip...
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet) {
printf("Received Packet\n");
}
/// ...snip...
pcap_dispatch(handle, 100, got_packet, NULL);
to warrant the compiler warnings that I am seeing:
gcc -g -O3 -I../sensor/ -c -o sensordatad.o sensordatad.c
main.c: In function ‘start_capture’:
main.c:96:27: warning: initialization from incompatible pointer type [enabled by default]
main.c:146:3: warning: passing argument 3 of ‘pcap_dispatch’ from incompatible pointer
type [enabled by default]
It might be that I'm being too fussy, it seems like every other piece of C code I have ever seen has a page full of compiler warnings when compiling, but I want to at least finish this code without warnings!
I've also tried doing this, to somehow typecast the function pointer, something I hadn't seen in any of the examples; but I felt it worth a shot:
pcap_handler callback = &got_packet;
/// ...snip...
pcap_dispatch(handle, 100, &callback, NULL);
This resulted in the equally perplexing error:
main.c: In function ‘start_capture’:
main.c:96:27: warning: initialization from incompatible pointer type [enabled by default]
It's the first time I've run across function pointers with special typedef'ed types, the man page for pcap_{loop,dispatch} reads:
NAME
pcap_loop, pcap_dispatch - process packets from a live capture or savefile
SYNOPSIS
#include
typedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,
const u_char *bytes);
int pcap_loop(pcap_t *p, int cnt,
pcap_handler callback, u_char *user);
int pcap_dispatch(pcap_t *p, int cnt,
pcap_handler callback, u_char *user);
Solved
The function signature should be:
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
I had:
void got_packet(u_char *args, struct pcap_pkthdr *header, const u_char *packet);
I suppose in some ways the error makes sense?! But anyway Solved.

Yet another uresolved external case

first time asking.
Here's the deal:
I have a helper class (at least now it's a class) that has several math functions, that I use throughout the project.
#ifndef CUSTOM_UTILS_H
#define CUSTOM_UTILS_H
//---------------------------------------------------------
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>
class cUtil {
public:
static int utilsRandom(int from, int to);
static double utilsRandom(double from, double to);
static double giveAngle(double x, double y);
static double FoV(double cx, double cy,
double fx, double fy,
double tx, double ty,
double radius);
};
//---------------------------------------------------------
#endif
Implementation:
#define _USE_MATH_DEFINES
#include "customUtils.h"
//---------------------------------------------------------
int cUtil::utilsRandom(int from, int to) {
if (from == to) {
return from;
}
return (rand() % (to - from)) + from;
}
//---------------------------------------------------------
double cUtil::utilsRandom(double from, double to) {
...
}
//---------------------------------------------------------
double cUtil::giveAngle(double x, double y) {
...
}
//---------------------------------------------------------
double cUtil::FoV(double cx, double cy,
double fx, double fy,
double tx, double ty,
double radius) {
...
}
//---------------------------------------------------------
(removed the 3 bodies to save space for the post)
Now, when I use it , let's say, in a class called 'creature' I include the customUtils.h file in the header of 'creature'. And use any of the 4 functions like so: cUtil::func_name().
Sometimes I get an unresolved external error such as
LNK2001: unresolved external symbol "public: static double __cdecl cUtil::utilsRandom
(double,double)" (?utilsRandom#cUtil##SANNN#Z) C:\Users\Rockstrongo\Documents\Projects
\nnEvo\nnEvo\net.obj
It appears for all functions in cUtil and for all classes that use those functions.
I said it sometimes appears, because it just does that - I'd be rebuilding the project and it would resurface. To scrub it again I would change some part of cUtils code, or the way it is included in other classes or anything that it would get it running again. For some time, cleaning->compiling the customUtils.cpp->then building the rest worked, but not any more.
To an untrained eye like mine this appears to be completely random and I'm all out off straws to grasp. I'm using Microsoft Visual Studio 2010. It's a console project using openGL and glut.
I see you overloaded cUtil::utilsRandom to use doubles and ints. It's ok except for one thing: You can not change the returned type. Both overloaded version must either return an int or a double. If you must have different returned types, then do not overload the function. Rather use different function names.

warning: XXXX has different visibility (default) in YYYY and (hidden) in ZZZZ

I am trying to make an iPhone app that uses OpenCV plus another C++ Library.
It seems to compile and link fine. It actually works.
Is just I want to get rid of this ugly warning:
ld: warning: std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)has different visibility (default) in /Users/nacho4d/Documents/Projects/iOS/iAR/opencv_device/lib/libcxcore.a(cxdatastructs.o) and (hidden) in /Users/nacho4d/Documents/Projects/iOS/iAR/build/iAR.build/Debug-iphoneos/iAR.build/Objects-normal/armv6/combination.o
What does it mean?, How can I solve it?
just in case, this is the header of combination class, from the library I mentioned.
//combination.h
typedef std::vector<int> combi;
typedef std::vector< combi > allcombi;
class Combination
{
public:
void Init(const int n, const int m);
allcombi::iterator begin();
allcombi::iterator end();
allcombi::const_iterator begin() const;
allcombi::const_iterator end() const;
private:
void Nest(int nest, int column, int n1, int n2, int k[], allcombi &result);
private:
allcombi m_data;
};
Thanks in advance
Ignacio
It seems libcxcore.a and combination.o are compiled with different symbol visibility options.
Read about symbol visibility there.
So, I guess you just need to compile combination.cpp with -fvisibility=default flag. If you use XCode, check "Symbols Hidden by Default" setting in "GCC - Code Generation" section. It should be unchecked for both projects.