Re: Running into problem with R-Swig wrappers

Posted by Duncan Temple Lang on
URL: http://quantlib.414.s1.nabble.com/Running-into-problem-with-R-Swig-wrappers-tp11141p11144.html

-----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-----