A MonetDB User Defined Function with two double parameters - user-defined-functions

I am trying to evaluate the performance of MonetDB for an analytical workload that contains a large amount of floating point calculations which are used in an aggregation.
I am trying to implement a C based UDF in MonetDB to achieve this and am running into an error. I am unsure of how to implement the function correctly based on my required signature which is
double f(double,double);
Firstly, I am using MonetDB-11.15.17 built from source on Ubuntu 13.04.
I have added to the following files in the ./sql/backends/monet5/UDF directory as shown:
udf.c:
str UDFtest(flt *ret,flt *_p1,flt *_p2)
{
*ret = *_p1+*_p2;
return MAL_SUCCEED;
}
udf.h:
udf_export str UDFtest(flt *,flt*,flt*);
udf.mal:
module udf;
command calc_test(one:flt,two:flt):flt
address UDFtest
comment "udf floating point test";
80_udf.sql:
create function calc_test(one double,two double)
returns double external name udf.calc_test;
I then ran , bootstrap; make; sudo make install; and typed the following at the mclient prompt:
declare f1 float;
declare f2 float;
set f1=0.1;
set f2=0.2;
select calc_test(f1,f2);
This results in the following error:
TypeException:user.s1_1[6]:'udf.calc_test' undefined in: _9:any := udf.calc_test(_5:dbl, _8:dbl)
program contains errors
I've tried to piece together what I can from the documentation and source but am now stuck.
Where have I gone wrong in this process?

I solved this. I mixed up my data types by presuming that the typedef flt was equivalent to double as float === double in SQL.
The flt type in C is equivalent to the "real" SQL type.
It should look like this:
udf.c
str UDFtest(dbl *ret,dbl *_p1,dbl *_p2)
{
*ret = *_p1+*_p2;
return MAL_SUCCEED;
}
udf.h
udf_export str UDFtest(dbl *,dbl*,dbl*);
udf.mal
module udf;
command calc_test(one:dbl,two:dbl):dbl
address UDFtest
comment "udf floating point test";
80_udf.sql
create function calc_test(one double,two double)
returns double external name udf.calc_test;

Related

A non well formed numeric value encountered in laravel

I am trying to add two variables together. I believe both contain an integer, but when I draw what is stored within $product->mileage, I receive the following error:
A non well formed numeric value encountered
$oilchange = $request->oilchange_at_kms;
$product = Product::find($request->product_id);
$mileage = $product->mileage; // Error within this variable, but it is an int
$total = $mileage + $oilchange;
How can I test this, or how can I find the problem in my code?
This error usually pops up when you try to add an integer with a string or some type of non numeric field.
You can test this by using the PHP gettype() method:
dump(gettype($product->mileage));
dd(gettype($oilchange));
If it turns out that one of these is a string (possibly from a form response), you can cast it to an int if you are certain that the value will always be an int.
$mileage = (int)$product->mileage;
Not really recommending this, as you should try to resolve the types within the variables first, but it may help you in testing.

Record non-scalar parameter in Omnet++

I am using a non-scalar parameter for my parameter study:
*.server.serviceTime = ${B=exponential(20ms), exponential(35ms)}
However, compared to the other scalar parameters, the B parameter is not shown in the Browse Data section of the results, which I was using until now to export the results of my parameter study:
How can I record the parameter of the exponential distribution (B) that I'm using?
The serviceTime is declared in the .ned as follows:
volatile double serviceTime #unit(s);
If I'm not mistaken you would like to record the mean value of the exponential distribution. Here is an example how the PureAlohaExperiment sample does this:
[Config PureAlohaExperiment]
...
Aloha.numHosts = ${numHosts=10,15,20}
Aloha.host[*].iaTime = exponential(${mean=1,2,3,4,5..9 step 2}s)
i.e. put the interation variable inside the exponential function.
You may put in a NED module a parameter called B. Then, you do the following in the omnetpp.ini:
**.B = ${B=exponential(20ms), exponential(35ms)}
Finally, you record the B NED parameter in the finish() function:
recordScalar("B", par("B"));
There is an option param-record-as-scalar for saving parameter as a scalar. An example of using it:
*.server.serviceTime.param-record-as-scalar = true
However, it doesn't work for volatile parameters (there is an error during finishing simulation). It seems that it is intentionally behaviour to avoid registering "meaningless" random values.
If you really need current random value of volatile parameter, you should record it as a new scalar just after reading it, for example:
double serviceTime = par("serviceTime").doubleValue();
recordScalar("serviceTime 1", serviceTime);
// ... later
serviceTime = par("serviceTime").doubleValue();
recordScalar("serviceTime 2", serviceTime);

Can I know data type from variable name in GCC?

I want to know data type using variable name
My final goal is getting a function signature for making a function stub(skeleton code)
but GCC error message just notify only undefined function name
Can I see a symbol table? (for inferencing function signature)
for example, foo.c is like below
#include <stdio.h>
int main() {
int n = 0;
n = foo();
return 0;
}
I want to make a function stub
so I want to know function foo has no parameter and returns an integer value
What should I do?
I think below:
linker error message say function foo is undefined
read line 5
n = foo();
inspect type of n using symbol table
is it right?
sorry for my bad english
please teach me inferencing a function signature
Inject his code into your source file:
typedef struct { int a; char c; } badtype_t;
badtype_t badtype;
then replace the error line like this:
n = badtype; //foo();
or if you want the type foo returns:
badtype = foo();
then you will get some error like this:
incompatible types when initializing type ‘int’ using type ‘badtype_t’
and you can get the type int.
or if you want the type of foo itself:
foo * 2
then you will get some error like this:
invalid operands to binary * (have 'int (*)()' and 'int')
and you can get the type int (*)() (that is, function taking nothing and returning an int).
It seems ok, but this strategy will not be good enough. Using the left-hand side of an expression is not enough to determine the return-type of the function. In particular, there may be no left-hand side at all, simply: foo();. What then?
If you just want to see a symbol table, that's what nm is for.
For example, if you get an error linking foo.o and bar.o together, you can do this:
nm -a foo.o
That will show you all the symbols defined in module foo.
But I don't see why you think this would help. C symbols do not have any type information. There may be enough metadata to distinguish extern linkage, and/or to tell whether a symbol function or data, but that's it. There is no way to tell an int from a float, or a function taking two ints and returning a double from a function taking a char * and returning a different char *.
So, you have some function named foo defined somewhere, and you want to know what its type is.
If you don't actually have a prototype for foo somewhere in your #included header files, this is easy:
If you're using C99, your code is invalid.
Otherwise, foo must take no arguments and return int, or your code is invalid.
And this isn't one of those "technically invalid, but it works on every platform" cases; it will break. For example, with gcc 4.2 for 64-bit x86 linux or Mac, if you do this:
double foo(double f) { return f*2; }
Then, without a header file, call it like this:
double f = foo(2.0);
printf("%f\n", f);
If compiled as C89, this will compile and link just fine (clang or gcc 4.8 will give you a warning; gcc 4.2 won't even do that by default), and run, and print out 2.0. At least on x86_64; on ARM7, you'll corrupt the stack, and segfault if you're lucky. (Of course it actually does double something—either your 2.0 or some random uninitialized value—but it can't return that to you; it's stashed it in an arbitrary floating-point register that the caller doesn't know to access.)
If it is in a header file, you can always search for it. emacs, graphical IDEs, etc. are very good at this. But you can use the compiler to help you out, in two ways.
First, just do this:
gcc -E main.c > main.i
less main.i
Now search for /foo, and you'll find it.
Or you can trick the compiler into giving you an error message, as in perreal's answer.

How to assign/bind a known function to a variable?

I wonder how to make a variable within IDA Pro bound to some function so the next time I double click the variable it will send me to the function.
v1 = this
*v2 = Known-Function
At Some Different location:
char __stdcall ClassA__KnownFunction(ClassA *ClassA, void a2) {
commands.....
}
I know you can set type to int, struct, dword etc. But I am looking for some method to point the variable to already known offset/function in IDA Pro.
Function pointer is merely a variable that holds the address of a function; you cannot treat a variable like a constant. You have two options:
Add the name of the function as a comment (just for the sake of documentation).
Get rid of the variable assignment, hard-code the function address by editing the hex, and then perform the analysis again.

System.AccessViolationException storing a variable with reflectio.emit

I'm building a compiler with reflection.emit in my spare time, and i've come to a problem that i'm not understanding.
A little context, I've a runtime with a couple of types and one of them is Float2, a simpler vector struct with two float values (X and Y). I've made a couple of properties that allow me to swizzle the values (a la hlsl). For example if i have a new Float2(1.0f, 2.0f), if i make something like (new Float2(1.0f, 2.0f)).YX i'm going to get a Float2(2.0f, 1.0f)
I'm using this type in my language and currently testing this case (minor details of the language omitted):
float2 a = float2(1.0, 2.0).yx;
return a;
I'm transforming float2(1.0, 2.0) in a new call and accessing the property YX of my Float2 type in .yx.
The problem is I'm getting a "System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.". I don't understand why because if I make something like this:
float2 a = float2(1.0, 2.0);
return a;
Everything goes well.
The IL code that i'm generating is the following (I think the problem occurs in "L_0014: stloc.0", I don't know why it happens though) :
.method public virtual final instance valuetype
[Bifrost.Psl]Bifrost.Psl.Compiler.Runtime.Float2 Main() cil managed
{
.maxstack 3
.locals init (
[0] valuetype [Bifrost.Psl]Bifrost.Psl.Compiler.Runtime.Float2 num)
L_0000: ldc.r4 1
L_0005: ldc.r4 2
L_000a: newobj instance void [Bifrost.Psl]Bifrost.Psl.Compiler.Runtime.Float2::.ctor(float32, float32)
L_000f: call instance valuetype [Bifrost.Psl]Bifrost.Psl.Compiler.Runtime.Float2 [Bifrost.Psl]Bifrost.Psl.Compiler.Runtime.Float2::get_XY()
L_0014: stloc.0
L_0015: ldloc.0
L_0016: ret
}
Result of peverify:
[IL]: Error: [offset 0x0000000F]
[found value 'Bifrost.Psl.Compiler.Runtime.Float2'][expected address of value 'Bifrost.Psl.Compiler.Runtime.Float2'] Unexpected type on the stack.
The IL looks OK, although I don't know what your Float2 looks like.
I found the best way to debug this is to save the assembly to disk, then run peverify. Any code that generates an AccessViolationException will cause an error in peverify.
Edit: The newobj doc on MSDN talks about pushing an object reference onto the stack, which I took to be a pointer to a value type. If you're getting this error from peverify then I think you need to
newobj
stloc to a temporary variable
ldloca to get the address of the value type stored in the temporary variable
call
Now that I think about it, this is what the C# compiler does if you do a direct call on a value type like 4.ToString();.

Resources