0

For me, one of the most interesting features in languages such as R or Scilab is the possibility of parallelizing operations by vectorizing functions ("meaning that the function will operate on all elements of a vector without needing to loop through and act on each element one at a time", in the words of The Carpentries). This is supposed to make the code clearer and faster to execute.

My question is: Is this a possibility in C or C++? Can we create functions in C that can operate either on a scalar or a vector? Can we use standard C functions as if they were vectorized?

Maybe C is so fast that you don't need this feature, but I want to be sure about this subject, since this would affect the way I translate algorithms into code.

To be more concrete, if I want to apply a function on each element of a vector in C, should I use a loop, or there are other alternatives?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Vicent
  • 313
  • 1
  • 3
  • 15
  • 1
    Most of R's actual work-horse-functions are written in C or C++. You could check the source. – wildplasser Jul 09 '21 at 15:21
  • @wildplasser Meaning? – Vicent Jul 09 '21 at 15:30
  • In C, functions are written for one type only. That can be some “incomplete” or hybrid type, and a function can be written that works on multiple types of data (such as scalars or arrays of scalars), but the code for the various cases the function supports has to be written manually, and it often involves kludges that lose the protection of the C type system and other features. – Eric Postpischil Jul 09 '21 at 15:33
  • 3
    That said, this is not a good question. It is too broad and comes from a point where the languages are not really understood. To learn about C, learn C. – Eric Postpischil Jul 09 '21 at 15:34
  • @EricPostpischil Thank you for your answer. I thought that my question had sense and was clear enough, since 'vectorization' and 'c' are two tags that are available in StackOverflow. It *is* a general question, but I think the meaning is clear. I edited the question, anyway, if this helps. – Vicent Jul 09 '21 at 15:41
  • C by itself is not "fast", but many compilers can translate C code to fast machine code (with proper compilation parameters and depending on the code). – chtz Jul 09 '21 at 15:57
  • 2
    In C, Operators cannot be overloaded. Your question makes sense in C++, not C. – Stéphane Mottelet Jul 09 '21 at 15:57
  • Just because a language have a syntax that allow short-hand notation for operating on bigger data structures, it doesn't mean that there isn't a loop in machine code. – Support Ukraine Jul 09 '21 at 16:29
  • 1
    If your code is written with vectorization in mind, a good compiler can autovectorize it without any explicit vectorization by the programmer. – EOF Jul 09 '21 at 16:48

1 Answers1

2

In (prior to ), a given "function call" cannot be overloaded. If you want a function that operates on a vector or a function that operates on an element, those functions should have different names.

With , _Generic and macros let you dispatch based on argument type. See this SO answer. That would permit sin(x) to do a scalar operation if x was a double, or a vector operation if x was not.

In functions can be overloaded. The same function (or operation) can do scalar operations on single elements and vector operations on multiple elements. You can also store results in auto type variables, so you can be agnostic to the return type.

Writing the glue code to convert a scalar operation into a vector one still has to be done somewhere, and C++ has only limited ability to automate writing that glue code.

Now, you could write style tagged unions that could contain either vectors or scalars and have the code that operates on them dynamically switch between the two modes.

In you could write template code that statically switches between vector and scalar implementations.

Both solutions are not something a beginner in either language would be able to successfully do.

has valarray, which does limited vectorization for you, but it isn't well supported by compilers, nor does it extend well.

Various libraries support efficient vectorization of a limited set of operations; any good matrix library, for example.

Most higher level (than C/C++) languages end up implementing their lower level high speed code in C or C++ or (in some cases) more directly in assembly. Usually C/C++ with assembly or "intrinsics" augmentation is enough to get the most of the performance speedup they want.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524