2

I am writing a signal processing program using matlab. I know there are two types of float-pointing variables, single and double. Considering the memory usage, I want my code to work with only single type variable when the system's memory is not large, while it can also be adapted to work with double type variables when necessary, without significant modification (simple and light modification before running is OK, i.e., I don't need runtime-check technique). I know this can be done by macro in C and by template in C++. I don't find practical techniques which can do this in matlab. Do you have any experience with this?

I have a simple idea that I define a global string containing "single" or "double", then I pass this string to any memory allocation method called in my code to indicate what type I need. I think this can work, I just want to know which technique you guys use and is widely accepted.

Hua
  • 184
  • 8
  • 1
    Note that the C and C++ techniques you mention are compile time only. If you are talking about distributing your software, and it varying based on the system where it runs, this needs to be decided at run time. And this is a correctness issue - if you don't need `double` precision, just use a `float`. If you do need `double` precision, then `float` still won't be good enough on a limited memory system. – BoBTFish May 12 '16 at 09:14
  • So you're _really_ asking about code generation in Matlab? Not single/double variables? – Lightness Races in Orbit May 12 '16 at 09:19
  • BTW "code" is an uncountable noun in this context; "codes" is wrong. – Lightness Races in Orbit May 12 '16 at 09:19
  • @BoBTFish Thanks for your reminder. I understand the difference you mentioned. I just need to change the variables type before running the code. For example, if I know in advance that the data to be processed is very large, I want to modify a "macro" in matlab codes to make the codes work with single variables to save memory. I accept a non-significant modification before running. – Hua May 12 '16 at 09:19
  • http://stackoverflow.com/q/17242291/560648 lol ew – Lightness Races in Orbit May 12 '16 at 09:20
  • @LightnessRacesinOrbit Thanks for your correction about "code":-) I will change it. What do you mean by "code generation" in Matlab? – Hua May 12 '16 at 09:22
  • @LightnessRacesinOrbit I am not familiar with the usage of "class" in matlab, but after going through the post quickly, I guess it uses a runtime technique, such as, calling "metaclass" in his code. Am I right? I think this is too heavy for my purpose:-( – Hua May 12 '16 at 09:38
  • @Hua: Yeah I have no idea what they're doing there. I'm not a Matlabber though. – Lightness Races in Orbit May 12 '16 at 09:39
  • I had a simple idea that I define a global string containing "single" or "double". Then I pass this string to any memory allocation method called in my code to indicate what type I need. I think this can work, I just want to know which technique you guys use and is widely accepted. – Hua May 12 '16 at 09:42
  • @Hua: This is a possible solution, the `cast` function might be helpful: `my_class='single';x=cast(3,my_class);`. – Daniel May 12 '16 at 10:01

1 Answers1

0

I cannot see how a template would help here. The type of c++ templates are still determined in compile time (std::vector vec ...). Also note that Matlab defines all variables as double by default unless something else is stated. You basically want runtime checks for your code. I can think of one solution as using a function with a persistent variable. The variable is set once per run. When you generate variables you would then have to generate all variables you want to have as float through this function. This will slow down assignment though, since you have to call a function to assign variables.

This example is somehow an implementation of the singleton pattern (but not exactly). The persistent variable type is set at the first use and cannot change later in the program (assuming that you do not do anything stupid as clearing the variable explicitly). I would recommend to go for hardcoding single in case performance is an issue, instead of having runtime checks or assignment functions or classes or what you can come up with.

function c = assignFloat(a,b)
persistent type;
if (isempty(type) & nargin==2)
    type = b;
elseif (isempty(type))
    type = 'single';
% elseif(nargin==2), error('Do not set twice!') % Optional code, imo unnecessary.
end
if (strcmp(type,'single'))
    c = single(a);
    return;
end
c = double(a);
end
patrik
  • 4,506
  • 6
  • 24
  • 48
  • Thanks for your answer:-) Maybe I should be more precise in my question. Actually I don't need to runtime check. I'd like rather a compile-time technique, which allows me to 'change' variables type before each running. – Hua May 12 '16 at 11:51
  • @Hua Well, that is not entirely accurate. In case the type depends on a value of variable and you check this variable when running the code, then it is runtime checked. Had it been a macro it would have been checked in compile time. Then the macro would be an alias for the variable and editing the macro would have been the same as a (pre)compile time check. However, Matlab does not have macros and thus you have to have a switch which you check at runtime. An alternative would be to have a function `assignFloat` which you edit every time. Change the value from single to double. – patrik May 12 '16 at 13:43
  • You are right. I guess your "assignFloat" is somehow similar to the global string I mentioned in the question post. (actually may be the same as your "persistent type"). If using a global string, we also edit/change the string before we run the code. Then the memory allocation method will allocate proper type memory according to the parameter passed by the string. – Hua May 12 '16 at 13:52
  • @Hua Apart from that if you used a global string, there is no way of determine where it is set. And that you actually had not said anything about how this string was meant to be used. Global variables can potentially cause a lot of damage. If you have do something like what you want you should retain the code to a function or class with the specific purpose of doing exactly this. All code should be contained inside this particular construct and should not be possible to modify from outside. If case you do not do this you will likely shoot yourself in the foot sooner or later. – patrik May 12 '16 at 18:13
  • I would actually like to do a minor update in the update for my comment about runtime checks. In c++ it is actually possible to assign a `constexpr` as well, which is actually evaluated at compile time. This is not possible either in Matlab. It is also possible to do a lot of complicated constructs to make things like this possible, but I would guess the most common way to do this in c/c++ is via aliasing. – patrik May 19 '16 at 06:48