caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* problem calling C-function from Ocaml
@ 2005-11-01  2:06 Dan Koppel
  2005-11-08 11:54 ` [Caml-list] " Matthieu Dubuget
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Koppel @ 2005-11-01  2:06 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2091 bytes --]

Hello all,
  I'm trying to use a C-function called from inside an Ocaml function.  I believe I'm using the correct syntax, but I'm getting some strange results.  Therefore something I'm doing must be wrong.
 
Here's my C-function:
 
float myCTime(int dummy) {
        float factor = 1.0;
        return (factor*((float) clock()));
}
 
Here's how it's being called from Ocaml:
 
let dummy = 0 in
print_float (myCamlTime dummy); (* first call *)
print_newline ();
some code here which always takes exactly 30 seconds
print_float (myCamlTime dummy); (* second call *)
print_newline ();
 
The output I get is:
0.
3000.
 
So, it seems that the time returned is in units of "centiseconds".  However, when I change the variable "factor" in the C-function to 2.0, the output is exactly the same!  I know this function IS being called because when I replace the last line with "return 0.0", the output changes (both calls return the same number).
 
Here is the C "stub function" that I use:
 
value stub_myCTime(
        value _v_dummy)
{
  int dummy; /*in*/
  float _res;
  value _vres;
  dummy = Int_val(_v_dummy);
  _res = myCTime(dummy);
  _vres = copy_double(_res);
  return _vres;
}
value stub_myCTime_bytecode(value * argv, int argn)
{
  return stub_myCTime(argv[0]);
}
 
and here is the .ml/.mli file that I use:
 
external myCamlTime : int -> float
        = "stub_myCTime_bytecode" "stub_myCTime"
 
But something even more perplexing occurs when I insert a printf statement in the C-function.  The output of both calls is now "0.".  I am totally baffled.
 
Could a kind soul please tell me what, if anything, I may be doing wrong?  All I seek, actually, is an Ocaml function that returns CPU time used by this process with a very good accuracy and very small granularity.  The Unix function "gettimeofday" is good but it returns actual (clock) time, rather than CPU time.
 
Any help is greatly appreciated!
 
Sincerely, Dan
 
PS. Changing float => double in the C-function does not help.

		
---------------------------------
 Yahoo! FareChase - Search multiple travel sites in one click.  

[-- Attachment #2: Type: text/html, Size: 3000 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] problem calling C-function from Ocaml
  2005-11-01  2:06 problem calling C-function from Ocaml Dan Koppel
@ 2005-11-08 11:54 ` Matthieu Dubuget
  0 siblings, 0 replies; 2+ messages in thread
From: Matthieu Dubuget @ 2005-11-08 11:54 UTC (permalink / raw)
  To: Dan Koppel; +Cc: caml-list

Dan Koppel a écrit :

> Here is the C "stub function" that I use:
>  
> value stub_myCTime(
>         value _v_dummy)
> {
>   int dummy; /*in*/
>   float _res;
>   value _vres;
>   dummy = Int_val(_v_dummy);
>   _res = myCTime(dummy);
>   _vres = copy_double(_res);
>   return _vres;
> }
> value stub_myCTime_bytecode(value * argv, int argn)
> {
>   return stub_myCTime(argv[0]);
> }
>  
> and here is the .ml/.mli file that I use:
>  
> external myCamlTime : int -> float
>         = "stub_myCTime_bytecode" "stub_myCTime"
>  

I did not try. Here is the stub function I would have written (no need
for a distinct bytecode/native stub function, since there are less than
5 parameters.

CAMLprim value stub_myCTime(value _v_dummy){
CAMLparam1(_v_dummy);
int dummy;
dummy = Int_val(_v_dummy);
CAMLreturn( copy_double(myCTime(dummy)) );
}

The only point I think of  that you should check is: are the CAMLparam1
and CAMLreturn macros mandatory in your case?
I think that you are using a code that was generated by camlidl, am I
right? If this is the case, did you modify it?

Salutations

Matthieu


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-11-08 11:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-01  2:06 problem calling C-function from Ocaml Dan Koppel
2005-11-08 11:54 ` [Caml-list] " Matthieu Dubuget

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).