bitand, bitor

classic Classic list List threaded Threaded
5 messages Options
Enrico Segre Enrico Segre
Reply | Threaded
Open this post in threaded view
|

bitand, bitor

Hi there,
the recent bug activity about bitand, bitor & the like made me wonder
and look a bit into the functions themselves. Maybe I miss something,
but why at all does one need these contraptions? As they are written,
besides argument parsing, now these functions use an absurd detour via
converting their arguments to "binary" string, then to ascii codes, then
make string substitutions, then again convert back to floats - all this
is certainly not performant. Why can't one just use int32(x) &
int32(y)?
Is there a consideration of MAXINT? (a float can represent a 52bit
integer without loss of precision, which is more than 32bit -- consider
dec2hex(2^53+[-2:2]) But even then, that could be treated appropriately
without waiting for int64, say, with

a=2^32
z=double(int32(x/a) & int32(y/a))*a
z=z+double(int32(x-z) & int32(y-z))

Enrico


Pierre MARECHAL Pierre MARECHAL
Reply | Threaded
Open this post in threaded view
|

Re: bitand, bitor

Hi Enrico,

These functions have been written by a developper that has left the team
and I don't why bitor() and bitand() are coded in this way.
For the time being, I've just dealed with argument check without paying
attention to the algorithm.
But you're totally right, I'll use & and | operators.

Thanks

Regards

Pierre



Enrico Segre a écrit :

> Hi there,
> the recent bug activity about bitand, bitor & the like made me wonder
> and look a bit into the functions themselves. Maybe I miss something,
> but why at all does one need these contraptions? As they are written,
> besides argument parsing, now these functions use an absurd detour via
> converting their arguments to "binary" string, then to ascii codes, then
> make string substitutions, then again convert back to floats - all this
> is certainly not performant. Why can't one just use int32(x) &
> int32(y)?
> Is there a consideration of MAXINT? (a float can represent a 52bit
> integer without loss of precision, which is more than 32bit -- consider
> dec2hex(2^53+[-2:2]) But even then, that could be treated appropriately
> without waiting for int64, say, with
>
> a=2^32
> z=double(int32(x/a) & int32(y/a))*a
> z=z+double(int32(x-z) & int32(y-z))
>
> Enrico
>  

--
===================================================
Pierre MARECHAL
INRIA - Centre de Recherche de Paris - Rocquencourt
Domaine de Voluceau - B.P. 105
78153 Le Chesnay Cedex
===================================================
Equipe-Projet Scilab
Bâtiment 1B - Bureau 008
Email : [hidden email]
===================================================

COLLETTE Yann COLLETTE Yann
Reply | Threaded
Open this post in threaded view
|

SciFunction

Hello,

I'm trying to interface an optimisation method under scilab (the
optimization method is in fortran and I'm writing the interface in C).
My question is: how to retrieve the output parameters of a call to
SciFunction ?
The following program is called several times during the run of the
optimization:

/////////////////////////////////////
//  First I create some parameters. The first one will contain the "x"
vector and the second one the gradient of the output (I do this to give
the correct size to the stack. If I don't do this, scilab says it
doesn't know the ibegin + 1 parameter).

  // Create the first variable: x
  n_x1_tmp = n_x;
  m_x1_tmp = m_x;
  CreateVar(ibegin,"d",&n_x1_tmp,&m_x1_tmp,&l_x1_tmp);
  for(i=0;i<ndv; i++) *dstk(l_x1_tmp+i) = *dstk(l_x+i);

  // Create a fake 2nd variable on the stack. This will allow to call
GetRhsVar to retrieve the 2nd output argument
  n_x2_tmp = 1;
  m_x2_tmp = 1;
  CreateVar(ibegin+1,"d",&n_x2_tmp,&m_x2_tmp,&l_x2_tmp);

///////////////////////////////////
// I call the scilab function via SciFunction


  // Call to the scilab function
  SciFunction(&ibegin,&l_fobj,&fobj_lhs,&fobj_rhs);
  if (Err>0)
    {
      sciprint("conmin: error when calling objective function\n");
      return 0;
    } /* End If */

  n1 = ndv + 2;

////////////////////////////////////
// Now I get the parameters via GetRhsVar. I hope that the size of the
ibegin and ibegin+1 parameters are set correctly by SciFunction (first
free the memory and then mallocated correctly to get the output parameters).
  // Get fobj
  GetRhsVar(ibegin,  "d",&n_fobj_tmp, &m_fobj_tmp, &l_fobj_tmp);
  // Get gradient of fobj
  GetRhsVar(ibegin+1,"d",&n_dfobj_tmp,&m_dfobj_tmp,&l_dfobj_tmp);

/////////////////////////////////////
// Finally, I store all the output parameters in temporary arrays
(because just after the objective function, I called the constraints
also via SciFunction).

  // Fill C2F(cnmn1).obj: objective function value
  C2F(cnmn1).obj = *dstk(l_fobj_tmp);

  // DF(N1) Analytic gradient of the objective function for the current
decision variables, X(I).  DF(I) contains the partial derivative
  //        of OBJ with respect to X(I).  Calculate DF(I), I = 1, NDV if
INFO = 3 or INFO = 4 and if NFDG = 0 or NFDG = 2.
  double * DF = (double *)malloc(n1*sizeof(double));
  // Fill DF(N1): gradient of objective function value
  for(j=0;j<ndv;j++) DF[j] = *dstk(l_dfobj_tmp+j);

///////////////////////////////////////
// Is my strategy good or do you recommend another method ?

Your sincerely,

Yann COLLETTE


-- Disclaimer ------------------------------------
Ce message ainsi que les eventuelles pieces jointes constituent une correspondance privee et confidentielle a l'attention exclusive du destinataire designe ci-dessus. Si vous n'etes pas le destinataire du present message ou une personne susceptible de pouvoir le lui delivrer, il vous est signifie que toute divulgation, distribution ou copie de cette transmission est strictement interdite. Si vous avez recu ce message par erreur, nous vous remercions d'en informer l'expediteur par telephone ou de lui retourner le present message, puis d'effacer immediatement ce message de votre systeme.
***
This e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.
Sylvestre Ledru Sylvestre Ledru
Reply | Threaded
Open this post in threaded view
|

Re: SciFunction

HI Yann,

I am not answering to your question (sorry).
But I would like to inform you that we changed the data type declaration
in CreateVar, GetRhsVar, etc for something less cryptic:

        + "c" => STRING_DATATYPE
        + "S" => MATRIX_OF_STRING_DATATYPE
        + "d" => MATRIX_OF_DOUBLE_DATATYPE
        + "r" => MATRIX_OF_RATIONAL_DATATYPE
        + "I" => MATRIX_OF_VARIABLE_SIZE_INTEGER_DATATYPE
        + "i" => MATRIX_OF_INTEGER_DATATYPE
        + "b" => MATRIX_OF_BOOLEAN_DATATYPE
        + "z" => MATRIX_OF_COMPLEX_DATATYPE
        + "s" => SPARSE_MATRIX_DATATYPE
        + "l" => LIST_DATATYPE
        + "t" => TYPED_LIST_DATATYPE
        + "m" => MATRIX_ORIENTED_TYPED_LIST_DATATYPE
        + "p" => SCILAB_POINTER_DATATYPE
        + "h" => GRAPHICAL_HANDLE_DATATYPE
        + "f" => EXTERNAL_DATATYPE
You will find the list here:
modules/core/includes/stackTypeVariable.h

Cheers,
Sylvestre

Le mercredi 06 février 2008 à 14:50 +0100, COLLETTE Yann a écrit :

> Hello,
>
> I'm trying to interface an optimisation method under scilab (the
> optimization method is in fortran and I'm writing the interface in C).
> My question is: how to retrieve the output parameters of a call to
> SciFunction ?
> The following program is called several times during the run of the
> optimization:
>
> /////////////////////////////////////
> //  First I create some parameters. The first one will contain the "x"
> vector and the second one the gradient of the output (I do this to give
> the correct size to the stack. If I don't do this, scilab says it
> doesn't know the ibegin + 1 parameter).
>
>   // Create the first variable: x
>   n_x1_tmp = n_x;
>   m_x1_tmp = m_x;
>   CreateVar(ibegin,"d",&n_x1_tmp,&m_x1_tmp,&l_x1_tmp);
>   for(i=0;i<ndv; i++) *dstk(l_x1_tmp+i) = *dstk(l_x+i);
>
>   // Create a fake 2nd variable on the stack. This will allow to call
> GetRhsVar to retrieve the 2nd output argument
>   n_x2_tmp = 1;
>   m_x2_tmp = 1;
>   CreateVar(ibegin+1,"d",&n_x2_tmp,&m_x2_tmp,&l_x2_tmp);
>
> ///////////////////////////////////
> // I call the scilab function via SciFunction
>
>
>   // Call to the scilab function
>   SciFunction(&ibegin,&l_fobj,&fobj_lhs,&fobj_rhs);
>   if (Err>0)
>     {
>       sciprint("conmin: error when calling objective function\n");
>       return 0;
>     } /* End If */
>
>   n1 = ndv + 2;
>
> ////////////////////////////////////
> // Now I get the parameters via GetRhsVar. I hope that the size of the
> ibegin and ibegin+1 parameters are set correctly by SciFunction (first
> free the memory and then mallocated correctly to get the output parameters).
>   // Get fobj
>   GetRhsVar(ibegin,  "d",&n_fobj_tmp, &m_fobj_tmp, &l_fobj_tmp);
>   // Get gradient of fobj
>   GetRhsVar(ibegin+1,"d",&n_dfobj_tmp,&m_dfobj_tmp,&l_dfobj_tmp);
>
> /////////////////////////////////////
> // Finally, I store all the output parameters in temporary arrays
> (because just after the objective function, I called the constraints
> also via SciFunction).
>
>   // Fill C2F(cnmn1).obj: objective function value
>   C2F(cnmn1).obj = *dstk(l_fobj_tmp);
>
>   // DF(N1) Analytic gradient of the objective function for the current
> decision variables, X(I).  DF(I) contains the partial derivative
>   //        of OBJ with respect to X(I).  Calculate DF(I), I = 1, NDV if
> INFO = 3 or INFO = 4 and if NFDG = 0 or NFDG = 2.
>   double * DF = (double *)malloc(n1*sizeof(double));
>   // Fill DF(N1): gradient of objective function value
>   for(j=0;j<ndv;j++) DF[j] = *dstk(l_dfobj_tmp+j);
>
> ///////////////////////////////////////
> // Is my strategy good or do you recommend another method ?
>
> Your sincerely,
>
> Yann COLLETTE
>
>
> -- Disclaimer ------------------------------------
> Ce message ainsi que les eventuelles pieces jointes constituent une correspondance privee et confidentielle a l'attention exclusive du destinataire designe ci-dessus. Si vous n'etes pas le destinataire du present message ou une personne susceptible de pouvoir le lui delivrer, il vous est signifie que toute divulgation, distribution ou copie de cette transmission est strictement interdite. Si vous avez recu ce message par erreur, nous vous remercions d'en informer l'expediteur par telephone ou de lui retourner le present message, puis d'effacer immediatement ce message de votre systeme.
> ***
> This e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.


ycollet ycollet
Reply | Threaded
Open this post in threaded view
|

Re: SciFunction

Bonne et heureuse idée. Ce sera beaucoup plus parlant.

YC

Sylvestre Ledru a écrit :

> HI Yann,
>
> I am not answering to your question (sorry).
> But I would like to inform you that we changed the data type declaration
> in CreateVar, GetRhsVar, etc for something less cryptic:
>
> + "c" => STRING_DATATYPE
> + "S" => MATRIX_OF_STRING_DATATYPE
> + "d" => MATRIX_OF_DOUBLE_DATATYPE
> + "r" => MATRIX_OF_RATIONAL_DATATYPE
> + "I" => MATRIX_OF_VARIABLE_SIZE_INTEGER_DATATYPE
> + "i" => MATRIX_OF_INTEGER_DATATYPE
> + "b" => MATRIX_OF_BOOLEAN_DATATYPE
> + "z" => MATRIX_OF_COMPLEX_DATATYPE
> + "s" => SPARSE_MATRIX_DATATYPE
> + "l" => LIST_DATATYPE
> + "t" => TYPED_LIST_DATATYPE
> + "m" => MATRIX_ORIENTED_TYPED_LIST_DATATYPE
> + "p" => SCILAB_POINTER_DATATYPE
> + "h" => GRAPHICAL_HANDLE_DATATYPE
> + "f" => EXTERNAL_DATATYPE
> You will find the list here:
> modules/core/includes/stackTypeVariable.h
>
> Cheers,
> Sylvestre
>
> Le mercredi 06 février 2008 à 14:50 +0100, COLLETTE Yann a écrit :
>  
>> Hello,
>>
>> I'm trying to interface an optimisation method under scilab (the
>> optimization method is in fortran and I'm writing the interface in C).
>> My question is: how to retrieve the output parameters of a call to
>> SciFunction ?
>> The following program is called several times during the run of the
>> optimization:
>>
>> /////////////////////////////////////
>> //  First I create some parameters. The first one will contain the "x"
>> vector and the second one the gradient of the output (I do this to give
>> the correct size to the stack. If I don't do this, scilab says it
>> doesn't know the ibegin + 1 parameter).
>>
>>   // Create the first variable: x
>>   n_x1_tmp = n_x;
>>   m_x1_tmp = m_x;
>>   CreateVar(ibegin,"d",&n_x1_tmp,&m_x1_tmp,&l_x1_tmp);
>>   for(i=0;i<ndv; i++) *dstk(l_x1_tmp+i) = *dstk(l_x+i);
>>
>>   // Create a fake 2nd variable on the stack. This will allow to call
>> GetRhsVar to retrieve the 2nd output argument
>>   n_x2_tmp = 1;
>>   m_x2_tmp = 1;
>>   CreateVar(ibegin+1,"d",&n_x2_tmp,&m_x2_tmp,&l_x2_tmp);
>>
>> ///////////////////////////////////
>> // I call the scilab function via SciFunction
>>
>>
>>   // Call to the scilab function
>>   SciFunction(&ibegin,&l_fobj,&fobj_lhs,&fobj_rhs);
>>   if (Err>0)
>>     {
>>       sciprint("conmin: error when calling objective function\n");
>>       return 0;
>>     } /* End If */
>>
>>   n1 = ndv + 2;
>>
>> ////////////////////////////////////
>> // Now I get the parameters via GetRhsVar. I hope that the size of the
>> ibegin and ibegin+1 parameters are set correctly by SciFunction (first
>> free the memory and then mallocated correctly to get the output parameters).
>>   // Get fobj
>>   GetRhsVar(ibegin,  "d",&n_fobj_tmp, &m_fobj_tmp, &l_fobj_tmp);
>>   // Get gradient of fobj
>>   GetRhsVar(ibegin+1,"d",&n_dfobj_tmp,&m_dfobj_tmp,&l_dfobj_tmp);
>>
>> /////////////////////////////////////
>> // Finally, I store all the output parameters in temporary arrays
>> (because just after the objective function, I called the constraints
>> also via SciFunction).
>>
>>   // Fill C2F(cnmn1).obj: objective function value
>>   C2F(cnmn1).obj = *dstk(l_fobj_tmp);
>>
>>   // DF(N1) Analytic gradient of the objective function for the current
>> decision variables, X(I).  DF(I) contains the partial derivative
>>   //        of OBJ with respect to X(I).  Calculate DF(I), I = 1, NDV if
>> INFO = 3 or INFO = 4 and if NFDG = 0 or NFDG = 2.
>>   double * DF = (double *)malloc(n1*sizeof(double));
>>   // Fill DF(N1): gradient of objective function value
>>   for(j=0;j<ndv;j++) DF[j] = *dstk(l_dfobj_tmp+j);
>>
>> ///////////////////////////////////////
>> // Is my strategy good or do you recommend another method ?
>>
>> Your sincerely,
>>
>> Yann COLLETTE
>>
>>
>> -- Disclaimer ------------------------------------
>> Ce message ainsi que les eventuelles pieces jointes constituent une correspondance privee et confidentielle a l'attention exclusive du destinataire designe ci-dessus. Si vous n'etes pas le destinataire du present message ou une personne susceptible de pouvoir le lui delivrer, il vous est signifie que toute divulgation, distribution ou copie de cette transmission est strictement interdite. Si vous avez recu ce message par erreur, nous vous remercions d'en informer l'expediteur par telephone ou de lui retourner le present message, puis d'effacer immediatement ce message de votre systeme.
>> ***
>> This e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.
>>    
>
>