Running into problem with R-Swig wrappers

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Running into problem with R-Swig wrappers

Joseph Wang-2
The problem I'm running into is that an overloaded r function with large
numbers of arguments seem to be eating up huge amounts of memory, causing my
512 meg machine to run out of free memory before loading the wrapper.  

The trouble is that the functions with large numbers of option arguments
generate r overloaded functions.  

One way I've been able to get things to work is by defining

#if defined(SWIGR)
#define OPTIONAL(x, y) x
#else
#define OPTIONAL(x, y) x = y
#endif

and then

        ParCouponPtr(Real nominal, const Date& paymentDate,
                     const XiborPtr& index,
                     const Date& startDate, const Date& endDate,
                     Integer fixingDays,
                     OPTIONAL(Spread spread, 0.0),
                     OPTIONAL(const Date& refPeriodStart, Date()),
                     OPTIONAL(const Date& refPeriodEnd, Date())

This forces R to remove the optional arguments, which means that R no longer
uses overloading to represent these long functions, which bypasses the memory
bloating.

I've e-mailed the r-help list for any suggestions to get around the memory
bloat.

I'm curious if anyone has any other ideas on how to handle this.  One thing
that might be possible is to turn off operator overloading in R-SWIG if there
are more than six arguments, and then to use a herustic to figure out which
method to generate a wrapper for.

Thoughts?



Reply | Threaded
Open this post in threaded view
|

Re: Running into problem with R-Swig wrappers

Duncan Temple Lang
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



Can you show us some of the overloaded functions, please?
I don't know what you mean by a "large" number of arguments
and why you are generating functions with a large number of
parameters so it would be helpful to see what you are generating.

 D.

Joseph Wang wrote:

> The problem I'm running into is that an overloaded r function with large
> numbers of arguments seem to be eating up huge amounts of memory, causing my
> 512 meg machine to run out of free memory before loading the wrapper.  
>
> The trouble is that the functions with large numbers of option arguments
> generate r overloaded functions.  
>
> One way I've been able to get things to work is by defining
>
> #if defined(SWIGR)
> #define OPTIONAL(x, y) x
> #else
> #define OPTIONAL(x, y) x = y
> #endif
>
> and then
>
>         ParCouponPtr(Real nominal, const Date& paymentDate,
>                      const XiborPtr& index,
>                      const Date& startDate, const Date& endDate,
>                      Integer fixingDays,
>                      OPTIONAL(Spread spread, 0.0),
>                      OPTIONAL(const Date& refPeriodStart, Date()),
>                      OPTIONAL(const Date& refPeriodEnd, Date())
>
> This forces R to remove the optional arguments, which means that R no longer
> uses overloading to represent these long functions, which bypasses the memory
> bloating.
>
> I've e-mailed the r-help list for any suggestions to get around the memory
> bloat.
>
> I'm curious if anyone has any other ideas on how to handle this.  One thing
> that might be possible is to turn off operator overloading in R-SWIG if there
> are more than six arguments, and then to use a herustic to figure out which
> method to generate a wrapper for.
>
> Thoughts?

- --
Duncan Temple Lang                    [hidden email]
Department of Statistics              work:  (530) 752-4782
4210 Mathematical Sciences Building   fax:   (530) 752-7099
One Shields Ave.
University of California at Davis
Davis,
CA 95616,
USA
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (Darwin)

iD8DBQFEhkxg9p/Jzwa2QP4RAp8zAJ9fq4+xV3cjD4+uhB8SkCk18JPUuACdEMA0
Fp5feNGFE2CmiGJrrkJ6cv4=
=w/8W
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|

Re: Running into problem with R-Swig wrappers

Joseph Wang-2
在 Tuesday 06 June 2006 22:48,您写道:
> Can you show us some of the overloaded functions, please?
> I don't know what you mean by a "large" number of arguments
> and why you are generating functions with a large number of
> parameters so it would be helpful to see what you are generating.
>

Sure thing the signature

        FixedCouponBondPtr(const Date& issueDate,
                           const Date& datedDate,
                           const Date& maturityDate,
                           Integer settlementDays,
                           const std::vector<Rate>& coupons,
                           Frequency couponFrequency,
                           const Calendar& calendar,
                           const DayCounter& dayCounter,
                           BusinessDayConvention accrualConvention
                                                       = QuantLib::Following,
                           BusinessDayConvention paymentConvention
                                                       = QuantLib::Following,
                           Real redemption = 100.0,
                           const Handle<YieldTermStructure>& discountCurve
                                              = Handle<YieldTermStructure>(),
                           const Date& stub = Date(),
                           bool fromEnd = true)

generates six separate wrappers for each possible number of optional argument.
(See below for the first few wrappers.)

Loading the wrappers into R seems to take about several meg of heap space.  
Loading in about a hundred of these sorts of functions, kills the machine.

If I change the signature to make the optional arguments required, then an
ordinary non-generic function gets generated, and I don't get the huge memory
bloat.



setGeneric('FixedCouponBond', function(s_arg1, s_arg2, s_arg3, s_arg4, s_arg5,
s_arg6, s_arg7, s_arg8, s_arg9, s_arg10, s_arg11, s_arg12, s_arg13, s_arg14,
s_arg15, ...) standardGeneric('FixedCouponBond'))
attr(FixedCouponBond, 'returnType') = '_p_FixedCouponBondPtr'
setMethod('FixedCouponBond',
c('_p_Date', '_p_Date', '_p_Date', 'numeric', '_p_std__vectorTdouble_std__allocatorTdouble_t_t', 'character', '_p_Calendar', '_p_DayCounter', 'character', 'character', 'numeric', '_p_HandleTYieldTermStructure_t', '_p_Date', 'logical',
s_arg15 = "missing"),
function(s_arg1, s_arg2, s_arg3, s_arg4, s_arg5, s_arg6, s_arg7, s_arg8,
s_arg9, s_arg10, s_arg11, s_arg12, s_arg13, s_arg14)
{
  s_arg1 = coerceIfNotSubclass(s_arg1, "_p_Date")
  s_arg2 = coerceIfNotSubclass(s_arg2, "_p_Date")
  s_arg3 = coerceIfNotSubclass(s_arg3, "_p_Date")
  s_arg4 = as.integer(s_arg4)
 
  if(length(s_arg4) > 1) {
    warning("using only the first element of s_arg4")
  }
 
  s_arg5 =
coerceIfNotSubclass(s_arg5, "_p_std__vectorTdouble_std__allocatorTdouble_t_t")
  s_arg6 = enumToInteger(s_arg6, "_Frequency")
 
  if(length(s_arg6) > 1) {
    warning("using only the first element of s_arg6")
  }
 
  s_arg7 = coerceIfNotSubclass(s_arg7, "_p_Calendar")
  s_arg8 = coerceIfNotSubclass(s_arg8, "_p_DayCounter")
  s_arg9 = enumToInteger(s_arg9, "_BusinessDayConvention")
 
  if(length(s_arg9) > 1) {
    warning("using only the first element of s_arg9")
  }
 
  s_arg10 = enumToInteger(s_arg10, "_BusinessDayConvention")
 
  if(length(s_arg10) > 1) {
    warning("using only the first element of s_arg10")
  }
 
  s_arg11 = as.numeric(s_arg11)
  s_arg12 = coerceIfNotSubclass(s_arg12, "_p_HandleTYieldTermStructure_t")
  s_arg13 = coerceIfNotSubclass(s_arg13, "_p_Date")
  s_arg14 = as.logical(s_arg14)
  ans = .Call('R_swig_new_FixedCouponBond__SWIG_0', s_arg1, s_arg2, s_arg3,
s_arg4, s_arg5, s_arg6, s_arg7, s_arg8, s_arg9, s_arg10, s_arg11, s_arg12,
s_arg13, s_arg14, PACKAGE='QuantLib_wrap')
  class(ans) <- "_p_FixedCouponBondPtr"
 
  ans
 
}

)
setMethod('FixedCouponBond',
c('_p_Date', '_p_Date', '_p_Date', 'numeric', '_p_std__vectorTdouble_std__allocatorTdouble_t_t', 'character', '_p_Calendar', '_p_DayCounter', 'character', 'character', 'numeric', '_p_HandleTYieldTermStructure_t', '_p_Date',
s_arg14 = "missing", s_arg15 = "missing"),
function(s_arg1, s_arg2, s_arg3, s_arg4, s_arg5, s_arg6, s_arg7, s_arg8,
s_arg9, s_arg10, s_arg11, s_arg12, s_arg13)
{
  s_arg1 = coerceIfNotSubclass(s_arg1, "_p_Date")
  s_arg2 = coerceIfNotSubclass(s_arg2, "_p_Date")
  s_arg3 = coerceIfNotSubclass(s_arg3, "_p_Date")
  s_arg4 = as.integer(s_arg4)
 
  if(length(s_arg4) > 1) {
    warning("using only the first element of s_arg4")
  }
 
  s_arg5 =
coerceIfNotSubclass(s_arg5, "_p_std__vectorTdouble_std__allocatorTdouble_t_t")
  s_arg6 = enumToInteger(s_arg6, "_Frequency")
 
  if(length(s_arg6) > 1) {
    warning("using only the first element of s_arg6")
  }
 
  s_arg7 = coerceIfNotSubclass(s_arg7, "_p_Calendar")
  s_arg8 = coerceIfNotSubclass(s_arg8, "_p_DayCounter")
  s_arg9 = enumToInteger(s_arg9, "_BusinessDayConvention")
 
  if(length(s_arg9) > 1) {
    warning("using only the first element of s_arg9")
  }
 
  s_arg10 = enumToInteger(s_arg10, "_BusinessDayConvention")
 
  if(length(s_arg10) > 1) {
    warning("using only the first element of s_arg10")
  }
 
  s_arg11 = as.numeric(s_arg11)
  s_arg12 = coerceIfNotSubclass(s_arg12, "_p_HandleTYieldTermStructure_t")
  s_arg13 = coerceIfNotSubclass(s_arg13, "_p_Date")
  ans = .Call('R_swig_new_FixedCouponBond__SWIG_1', s_arg1, s_arg2, s_arg3,
s_arg4, s_arg5, s_arg6, s_arg7, s_arg8, s_arg9, s_arg10, s_arg11, s_arg12,
s_arg13, PACKAGE='QuantLib_wrap')
  class(ans) <- "_p_FixedCouponBondPtr"
 
  ans
 
}

)


Reply | Threaded
Open this post in threaded view
|

Re: Running into problem with R-Swig wrappers

Duncan Temple Lang
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


So am I correct in saying that in this example a single
C++ routine gives rise to 6 separate calls to setMethod()
in R?

If so, there is no need for that.  If there is only
method/routine then it corresponds to only one
R function and the S4 dispatch is an unnecessary
distraction and overwhelming performance inhibitor.

If you have polymorphic C++ routines/methods
which have optional arguments that are used to determine
the dispatch, then the problem becomes slightly more complex.

 D.


Joseph Wang wrote:

> 在 Tuesday 06 June 2006 22:48,您写道:
>
>>Can you show us some of the overloaded functions, please?
>>I don't know what you mean by a "large" number of arguments
>>and why you are generating functions with a large number of
>>parameters so it would be helpful to see what you are generating.
>>
>
>
> Sure thing the signature
>
>         FixedCouponBondPtr(const Date& issueDate,
>                            const Date& datedDate,
>                            const Date& maturityDate,
>                            Integer settlementDays,
>                            const std::vector<Rate>& coupons,
>                            Frequency couponFrequency,
>                            const Calendar& calendar,
>                            const DayCounter& dayCounter,
>                            BusinessDayConvention accrualConvention
>                                                        = QuantLib::Following,
>                            BusinessDayConvention paymentConvention
>                                                        = QuantLib::Following,
>                            Real redemption = 100.0,
>                            const Handle<YieldTermStructure>& discountCurve
>                                               = Handle<YieldTermStructure>(),
>                            const Date& stub = Date(),
>                            bool fromEnd = true)
>
> generates six separate wrappers for each possible number of optional argument.
> (See below for the first few wrappers.)
>
> Loading the wrappers into R seems to take about several meg of heap space.  
> Loading in about a hundred of these sorts of functions, kills the machine.
>
> If I change the signature to make the optional arguments required, then an
> ordinary non-generic function gets generated, and I don't get the huge memory
> bloat.
>
>
>
> setGeneric('FixedCouponBond', function(s_arg1, s_arg2, s_arg3, s_arg4, s_arg5,
> s_arg6, s_arg7, s_arg8, s_arg9, s_arg10, s_arg11, s_arg12, s_arg13, s_arg14,
> s_arg15, ...) standardGeneric('FixedCouponBond'))
> attr(FixedCouponBond, 'returnType') = '_p_FixedCouponBondPtr'
> setMethod('FixedCouponBond',
> c('_p_Date', '_p_Date', '_p_Date', 'numeric', '_p_std__vectorTdouble_std__allocatorTdouble_t_t', 'character', '_p_Calendar', '_p_DayCounter', 'character', 'character', 'numeric', '_p_HandleTYieldTermStructure_t', '_p_Date', 'logical',
> s_arg15 = "missing"),
> function(s_arg1, s_arg2, s_arg3, s_arg4, s_arg5, s_arg6, s_arg7, s_arg8,
> s_arg9, s_arg10, s_arg11, s_arg12, s_arg13, s_arg14)
> {
>   s_arg1 = coerceIfNotSubclass(s_arg1, "_p_Date")
>   s_arg2 = coerceIfNotSubclass(s_arg2, "_p_Date")
>   s_arg3 = coerceIfNotSubclass(s_arg3, "_p_Date")
>   s_arg4 = as.integer(s_arg4)
>  
>   if(length(s_arg4) > 1) {
>     warning("using only the first element of s_arg4")
>   }
>  
>   s_arg5 =
> coerceIfNotSubclass(s_arg5, "_p_std__vectorTdouble_std__allocatorTdouble_t_t")
>   s_arg6 = enumToInteger(s_arg6, "_Frequency")
>  
>   if(length(s_arg6) > 1) {
>     warning("using only the first element of s_arg6")
>   }
>  
>   s_arg7 = coerceIfNotSubclass(s_arg7, "_p_Calendar")
>   s_arg8 = coerceIfNotSubclass(s_arg8, "_p_DayCounter")
>   s_arg9 = enumToInteger(s_arg9, "_BusinessDayConvention")
>  
>   if(length(s_arg9) > 1) {
>     warning("using only the first element of s_arg9")
>   }
>  
>   s_arg10 = enumToInteger(s_arg10, "_BusinessDayConvention")
>  
>   if(length(s_arg10) > 1) {
>     warning("using only the first element of s_arg10")
>   }
>  
>   s_arg11 = as.numeric(s_arg11)
>   s_arg12 = coerceIfNotSubclass(s_arg12, "_p_HandleTYieldTermStructure_t")
>   s_arg13 = coerceIfNotSubclass(s_arg13, "_p_Date")
>   s_arg14 = as.logical(s_arg14)
>   ans = .Call('R_swig_new_FixedCouponBond__SWIG_0', s_arg1, s_arg2, s_arg3,
> s_arg4, s_arg5, s_arg6, s_arg7, s_arg8, s_arg9, s_arg10, s_arg11, s_arg12,
> s_arg13, s_arg14, PACKAGE='QuantLib_wrap')
>   class(ans) <- "_p_FixedCouponBondPtr"
>  
>   ans
>  
> }
>
> )
> setMethod('FixedCouponBond',
> c('_p_Date', '_p_Date', '_p_Date', 'numeric', '_p_std__vectorTdouble_std__allocatorTdouble_t_t', 'character', '_p_Calendar', '_p_DayCounter', 'character', 'character', 'numeric', '_p_HandleTYieldTermStructure_t', '_p_Date',
> s_arg14 = "missing", s_arg15 = "missing"),
> function(s_arg1, s_arg2, s_arg3, s_arg4, s_arg5, s_arg6, s_arg7, s_arg8,
> s_arg9, s_arg10, s_arg11, s_arg12, s_arg13)
> {
>   s_arg1 = coerceIfNotSubclass(s_arg1, "_p_Date")
>   s_arg2 = coerceIfNotSubclass(s_arg2, "_p_Date")
>   s_arg3 = coerceIfNotSubclass(s_arg3, "_p_Date")
>   s_arg4 = as.integer(s_arg4)
>  
>   if(length(s_arg4) > 1) {
>     warning("using only the first element of s_arg4")
>   }
>  
>   s_arg5 =
> coerceIfNotSubclass(s_arg5, "_p_std__vectorTdouble_std__allocatorTdouble_t_t")
>   s_arg6 = enumToInteger(s_arg6, "_Frequency")
>  
>   if(length(s_arg6) > 1) {
>     warning("using only the first element of s_arg6")
>   }
>  
>   s_arg7 = coerceIfNotSubclass(s_arg7, "_p_Calendar")
>   s_arg8 = coerceIfNotSubclass(s_arg8, "_p_DayCounter")
>   s_arg9 = enumToInteger(s_arg9, "_BusinessDayConvention")
>  
>   if(length(s_arg9) > 1) {
>     warning("using only the first element of s_arg9")
>   }
>  
>   s_arg10 = enumToInteger(s_arg10, "_BusinessDayConvention")
>  
>   if(length(s_arg10) > 1) {
>     warning("using only the first element of s_arg10")
>   }
>  
>   s_arg11 = as.numeric(s_arg11)
>   s_arg12 = coerceIfNotSubclass(s_arg12, "_p_HandleTYieldTermStructure_t")
>   s_arg13 = coerceIfNotSubclass(s_arg13, "_p_Date")
>   ans = .Call('R_swig_new_FixedCouponBond__SWIG_1', s_arg1, s_arg2, s_arg3,
> s_arg4, s_arg5, s_arg6, s_arg7, s_arg8, s_arg9, s_arg10, s_arg11, s_arg12,
> s_arg13, PACKAGE='QuantLib_wrap')
>   class(ans) <- "_p_FixedCouponBondPtr"
>  
>   ans
>  
> }
>
> )

- --
Duncan Temple Lang                    [hidden email]
Department of Statistics              work:  (530) 752-4782
4210 Mathematical Sciences Building   fax:   (530) 752-7099
One Shields Ave.
University of California at Davis
Davis,
CA 95616,
USA
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (Darwin)

iD8DBQFEhlUk9p/Jzwa2QP4RAv59AJ4s8CvIfa1TZIt9sGy8BL7sEF7l4wCdFSER
Mh7uiv4AY6HzRgxoC+1B6/U=
=GlVj
-----END PGP SIGNATURE-----