14
\$\begingroup\$

Currently, there are 62 questions tagged , nearly all of which say "The performance sucks; how can I vectorize this code?".

Recently, How to efficiently apply correction to a time series with fully vectorized code in Matlab was closed, with this comment:

This question is off-topic because you have reviewed your own code, and decided that it needs to be vectorized to make it work better. Once you have vectorized the code you can bring that code back for review. Alternatively, we can review the code you currently have, but reviewers may just recommend that you vectorize it - which you already know. Note, we review the code you have, not the code you want to have. – rolfl♦

How is this question different from the 61 other questions? Should vectorization requests be considered off-topic altogether? Or are there specific guidelines as to what constitutes a valid vectorization question on this site?

\$\endgroup\$
2
  • 5
    \$\begingroup\$ Saving this one until that diamond disappeared ;) \$\endgroup\$ Apr 19, 2018 at 21:13
  • \$\begingroup\$ Based on the majority view on this, I've changed the excerpt to "Use this tag if your code contains vectorized operations - where functions operate on whole arrays in one go. If it doesn't, but you want it to, then you should make those changes (perhaps with help from elsewhere) before presenting code for review." We might want something similar for the other paradigm tags such as object-oriented, functional etc. \$\endgroup\$ Jun 11, 2023 at 6:42

6 Answers 6

16
\$\begingroup\$

I'm going to come off a bit harsh with this, but wow does that tag need help.


I think that, in the case of , the tag itself has a lot of problems.

First off, the tag-usage wiki doesn't actually tell anything about how to use it. The hover is useless:

Vectorization refers to a programming paradigm where functions operate on whole arrays in one go. This affords benefits in terms of function calls, memory access, parallelization and code expressiveness. Some programming languages, such as MATLAB, are optimised to give the best performance when vectorized.

And then the actual Wiki:

Vectorization refers to a programming paradigm where the process of loop-based, scalar-oriented code is instead written using matrix and vector operations. Vectorization has the following benefits:

  • Performance: Vectorized code has better performance regarding function calls and memory access, and as a result, often runs much faster than the corresponding code containing loops.

  • Appearance: Vectorized code appears more like the textbook mathematical expressions, making the code more comprehensible.

  • Less Error Prone: vectorized code is shorter than loop based code, hence there are fewer opportunities to introduce programming bugs.

Some programming languages, in particular MATLAB, are optimised to give the best performance when vectorized.

I mean, what is that supposed to tell anyone?

If we want to keep the tag (which I personally suggest burning it due to the confusion it causes), we should reword the hover summary to talk about when to use it, and I think it should only be used if the code is already vectorized. If the question is about currently-vectorized code, the tag applies, and that's it. If the OP wants to vectorize it, well, they can post a self-answer with the vectorization, or tag it with the hopes that someone will vectorize it for them.

As with all questions, they can talk about what they want to see, but the should only be used when the question has vectorized code.

\$\endgroup\$
12
\$\begingroup\$

Specific question

Since I hammer-closed the question, and I added the comment, let me explain the reasoning I had, starting with a hypothetical case:


Hypothetically, someone asks a question: "this is my code, it works..."...

I then answer that question with: "Your code is very procedural and it would be much better if it was object-oriented"

That would make for a decent code-review question/answer.

If the OP then asks: "Can you make it object-oriented for me?" then it is no longer a code-review, but a "gimme-the-code" question.


In this case, the same thing has happened, except the OP has done a self-review.... they have looked at their code, and their code's performance, and identified an issue: "If this code was vectorized, it would be more efficient!". They are now asking: "Please vectorize my code".

The question is not asking for a review. It is asking for code, and in my opinion, it is off-topic.

Now, having said that, if they successfully vectorized the code, we would be on-topic to review that working code too.

Vectorization in general

I don't consider the tag to be off-topic in general. I think applying the tag to code containing vectorized operations to be a good thing.

I think the statement that the current tag is misleading, is overstating the problem. The current tag is:

Vectorization refers to a programming paradigm where functions operate on whole arrays in one go. This affords benefits in terms of function calls, memory access, parallelization and code expressiveness. Some programming languages, such as MATLAB, are optimised to give the best performance when vectorized.

I find that tag info to be clear, and accurately represent what vectorization is. Similar terms are "Object Orientation", "Recursion", etc.

The concern that the tag implies that people can ask to have their non-vectorized code to be converted to vectorized code is unfounded. That would be similar to worrying that people who tag their question with want their code converted to use linked lists.

Having said that, a scan of recent questions tagged with shows that many questions are incorrectly tagged. A good example is:

Find the minimum value that data could have had before it was rounded which I feel should be untagged (and currently has 4 close-votes - probably because of kick-back from this meta post). Note that the answer reviews the actual code and recommends recursion and not vectorization.... This is a good example of a mis-tagged question.

Just because people don't read the tag wiki does not make the tag bad. This is simply a case of un-tagging the tag from questions that don't require it.

I have edited the tag to add the text: Use this tag if your code contains vectorized operations

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I like it - brief, but to the point. :) (As those usage summaries should be.) \$\endgroup\$ Apr 19, 2018 at 20:05
  • \$\begingroup\$ I am downvoting because I disagree with the close reason - see my answer for that. \$\endgroup\$ Apr 20, 2018 at 8:39
6
\$\begingroup\$

rolfl's argument is that "I would like to vectorize this code to make it more efficient. Does anyone see a way to do this?" is off-topic because it asks for the code.

I disagree with this. "How do I vectorize this code" is a specific refactoring question (I believe this has been brought up on meta before but I have not yet found it). I honestly don't think we should differentiate between "How can I vectorize this code?" and "How can I make this code more object-oriented?" or "How do I write this code on just one line?" or "How can I make this code ?" In all cases, the author has done a self-review, they know what the problem with their code is.

If you argue that the question becomes on-topic if they ask for "How can I increase the performance of the code?" instead of asking for how to vectorize it specifically, I'm going to say that I don't think we should be so pedantic and require such a simple edit to a question to be the difference between on-topic and off-topic. The goal on code review is always: Make the code better/faster/harder/stronger (in some order).

Let's take a look at our on-topic questions, shall we?

  • Is code included directly in my question? Yes
  • Am I an author or maintainer of the code? Yes
  • Is it actual code from a project rather than pseudo-code or hypothetical code? Yes
  • To the best of my knowledge, does the code work as intended? The current code, yes. It works, but needs refactoring or performance boost. Doesn't make it off-topic.
  • Do I want the code to be good code? Yes
  • Do I want feedback about any or all facets of the code? A lot of our questions don't but we give feedback about all facets of the code anyway.
\$\endgroup\$
11
  • 2
    \$\begingroup\$ In this case, the question is off-topic because it fails the "any or all facets of the code".... the question could be made on-topic by removing the request to vectorize the scalar code, and the tag abuse could be resolved by removing the vectorization tag. Editing the question could perhaps bring it on topic again, but the change is too radical to present it as the OP's original post. \$\endgroup\$
    – rolfl
    Apr 19, 2018 at 21:06
  • 1
    \$\begingroup\$ @rolfl A lot of questions currently fails the "any or all facets of the code" and yet are not closed. How does this question differ from "How can I make this code more object-oriented"? \$\endgroup\$ Apr 19, 2018 at 21:26
  • 1
    \$\begingroup\$ We need to separate out the issue of on-topic from the issue of tag misuse. It's hard to treat them together. \$\endgroup\$
    – rolfl
    Apr 19, 2018 at 21:43
  • 3
    \$\begingroup\$ This specific question does not ask for any code review. It uses the code as a "spec", and says "I need the same thing but vectorized". It does not want any form of review on the code given in the post. In fact, it ends: "Here is a working example using a for loop .... How can I vectorize this problem?" \$\endgroup\$
    – rolfl
    Apr 19, 2018 at 21:46
  • \$\begingroup\$ @rolfl And how is that any different from "I need the same thing but object-oriented?" and where is this line that determines whether specific-refactoring questions are on-topic or not? \$\endgroup\$ Apr 19, 2018 at 21:48
  • 5
    \$\begingroup\$ It is no different from "I need the same thing but object-oriented".... \$\endgroup\$
    – rolfl
    Apr 19, 2018 at 21:48
  • \$\begingroup\$ I think you're right though - it's pretty pedantic. But, I think that's a different thing altogether - "this loop is slow, how can I speed it up" vs "How can I make this recursive?" That does seem to matter here. \$\endgroup\$ Apr 20, 2018 at 3:25
  • 2
    \$\begingroup\$ @Raystafarian If the difference between an off-topic question and an on-topic question is as simple as editing the pure-english part that the OP most likely haven't given very much thought, should we then tell the OPs "Sorry, you are asking for how to change your code - if you change it to <some other formulation> it would be on-topic."? That's not a comment I am willing to write. \$\endgroup\$ Apr 20, 2018 at 8:38
  • \$\begingroup\$ @SimonForsberg that's my point, I wouldn't want to leave that either, but sometimes it seems that's the way the site leans \$\endgroup\$ Apr 20, 2018 at 10:35
  • 2
    \$\begingroup\$ @Raystafarian The question we should be asking is if that's the way we want the site to go. \$\endgroup\$ Apr 20, 2018 at 11:35
  • 2
    \$\begingroup\$ Yeah, maybe we should be asking that \$\endgroup\$ Apr 20, 2018 at 23:53
2
\$\begingroup\$

I suggest that we put an end to the because the naming is misleading. The tag is misleading because it leads users to think that we offer help in vectorizing code, GIMME TEH CODEZ, which we don't do.

I think that we should create a new tag, Vectorized, that lets reviewers know, "hey, this code is vectorized code" or "I want my Vectorization reviewed"

I have also posted a Meta for the Burnination and Creation --> Should we Vectorize [tag:vectorization] questions with a [Vectorized] tag?

and to 202_accepted's point, new tag = new description

\$\endgroup\$
3
  • \$\begingroup\$ BURN IT TO THE GROUND \$\endgroup\$ Apr 19, 2018 at 19:54
  • 6
    \$\begingroup\$ No, it's a good tag. Just because some people have misused it does not make it bad. \$\endgroup\$
    – rolfl
    Apr 19, 2018 at 19:57
  • \$\begingroup\$ Read your edit and agree, @rolfl \$\endgroup\$
    – Malachi Mod
    Apr 19, 2018 at 20:05
2
\$\begingroup\$

Generally, questions where the code is a spec and the desired answer is new code belong on Stack Overflow, not Code Review, right? Unless I'm misunderstanding the purpose of cr.SE, I thought the code in the question was supposed to be the asker's current best attempt at what they really want.

Vectorization questions asking for different code are generally a good fit for Stack Overflow, though, and there are lot of existing duplicates in case of problems with already-posted solutions. That's another feature of this type of question: they can be duplicates despite the other question having different code or no code, obviously unlike a code review.


For SIMD vectorization using intrinsics in languages like C++, SO questions like Can counting byte matches between two strings be optimized using SIMD? are a good example: a simple C loop in the question, and answers that show SSE2-specific techniques to vectorize the sum of compare results. That's what the OP wants when they ask a question like that, not commentary on the coding-style or details of the scalar version. (Unless there's a way to tweak it so it auto-vectorizes nicely or at least acceptably, like in Optimizing char array iteration in C for intel processors on codereview and cross-posted to Stack Overflow.)

But Stack Overflow answers can also cover tweaks to get something to auto-vectorize, as in https://stackoverflow.com/questions/74415598/counting-differences-between-2-buffers-seems-too-slow

In Matlab and other high-level languages, vectorization means working with whole arrays, not looping manually over their elements. In languages that are lower-level, like C, Rust, C#, maybe even Java, manual SIMD vectorization means using intrinsics like _mm_add_epi32 on types like __m128i to ask the compiler to add 4x 32-bit integers with a single CPU instruction. (in that case, x86 paddd). This gets more interesting when you have arrays of different element widths, like in that mismatch-counting case where you have 8-bit elements to compare, but want a 32 or 64-bit sum of (mis)matches to avoid overflow with more than 256 bytes. Compilers often do a bad job, widening too early, so only processing a couple elements at a time instead of 16 or 32.

Anyway, Stack Overflow has quite a lot of questions about manually vectorizing various problems, so duplicates can get closed more easily there. And multiple experts keep an eye on SO's [simd], [sse], [avx], [neon], and other tags, so that's a good place for such questions.

Questions with no effort towards vectorizing the problem themselves do sometimes get a poor reception, especially if the problem seems pretty specific to them, unlikely to be generally applicable. (Counting matching or mismatching bytes is a general and interesting enough problem that people were willing to answer it without expecting the OP to have made an attempt at it themselves. But it's also good or even preferred when a question does have a working SIMD implementation and asks for how to speed it up, as in How to count character occurrences using SIMD. That could work as a code-review question, but IMO belongs on Stack Overflow along with other [simd][avx2] questions.)

\$\endgroup\$
3
  • \$\begingroup\$ I'm unsure if your answer will achieve the results you intend. The question and answers were posted in 2018, whilst rolfl and Simon Forsberg clearly disagree and the two answers don't really have a clear 'winner' the discussion seems quite settled. Additionally the number of active Meta users is significantly lower now-a-days, so you're unlikely to get the votes to 'win' the debate. If you want to restart the conversation I think asking a new question would be better. \$\endgroup\$
    – Peilonrayz Mod
    Jun 11, 2023 at 3:07
  • \$\begingroup\$ On a side note I appreciate you're a very well established SO user, so I am confident in your knowledge of SO. However I find talking about SO somewhat muddies the water. I could be wrong, however I'm confident I've seen SO users suggest OPs post on Code Review for performance and vectorization questions. Whilst I appreciate you're likely more right here than lower rep SO users (which makes up much of CR). I think talking about what's more appropriate for SO than CR can be confusing. \$\endgroup\$
    – Peilonrayz Mod
    Jun 11, 2023 at 3:11
  • 2
    \$\begingroup\$ @Peilonrayz: Good point that I ended up writing a bunch about whether questions like this fit on SO. I guess I was thinking that since there is another site where these questions are on-topic, there isn't even that reason to bend the CR rules to accommodate them. And to give guidance on when to recommend a user re-post on SO instead of CR, when what they're really looking for is a totally implementation from what they posted. I don't have any significant goals for achieving anything with this post, just putting in my 2 cents here instead of replying in an off-topic comment thread. \$\endgroup\$ Jun 11, 2023 at 3:50
1
\$\begingroup\$

A thought crossed my mind. Simon asked:

How does this question differ from "How can I make this code more object-oriented"?

To my mind I see some difference, though I don't know whether it's enough to matter. With a question like "how can I make this code more object-oriented," it's possible to give instruction or advice on that topic without directly writing the code for them. With vectorization, that seems a little more difficult.

For example, on a question like "how can I make this code more object-oriented," I could probably give high-level advice like, "Rather than using a separate x and y variable, you could make a Point class for holding positions. A Point class might have methods for adding or subtracting points or adding a vector to a point and other common actions." I probably wouldn't write an entire new class into an answer, though I might put a few snippets in.

For "how do I vectorize this code," I feel like there are fewer high level answers to that question. If you're iterating over a 2D array, you can probably give advice on whether iterating over columns first or rows first would be a better idea and why, but I feel like a lot more of it is just writing the code. But that's just a feeling based on my experience in vectorizing code.

Does this distinction matter, or is it a distinction without a difference? Does the degree of high-level overview vs. writing actual code matter? Is one preferred over the other? My guess is that it doesn't matter, but I thought I'd put it out there to see what others think.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Note that vectorization in Matlab is quite different to vectors and points in other math and physics contexts: See some documentation. Having said that, I believe your basic point (no pun intended) is still valid. \$\endgroup\$
    – rolfl
    Apr 22, 2018 at 10:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .