C++ Adding 2 arrays together quickly

前端 未结 6 1679
一整个雨季
一整个雨季 2021-02-04 16:57

Given the arrays:

int canvas[10][10];
int addon[10][10];

Where all the values range from 0 - 100, what is the fastest way in C++ to add

相关标签:
6条回答
  • 2021-02-04 17:20

    You should check out CUDA. This kind of problem is right up CUDA's street. Recommend the Programming Massively Parallel Processors book.

    However, this does require CUDA capable hardware, and CUDA takes a bit of effort to get setup in your development environment, so it would depend how important this really is!

    Good luck!

    0 讨论(0)
  • 2021-02-04 17:22

    The best you're going to do in standard C or C++ is to recast that as a one-dimensional array of 100 numbers and add them in a loop. (Single subscripts will use a bit less processing than double ones, unless the compiler can optimize it out. The only way you're going to know how much of an effect there is, if there is one, is to test.)

    You could certainly create a class where the addition would be one simple C++ instruction (canvas += addon;), but that wouldn't speed anything up. All that would happen is that the simple C++ instruction would expand into the loop above.

    You would need to get into lower-level processing in order to speed that up. There are additional instructions on many modern CPUs to do such processing that you might be able to use. You might be able to run something like this on a GPU using something like Cuda. You could try making the operation parallel and running on several cores, but on such a small instance you'll have to know how caching works on your CPU.

    The alternatives are to improve your algorithm (on a knapsack-type problem, you might be able to use dynamic programming in some way - without more information from you, we can't tell you), or to accept the performance. Tens of millions of operations on a 10 by 10 array turn into hundreds of billions of operations on numbers, and that's not as intimidating as it used to be. Of course, I don't know your usage scenario or performance requirements.

    0 讨论(0)
  • 2021-02-04 17:28

    Two parts: first, consider your two-dimensional array [10][10] as a single array [100]. The layout rules of C++ should allow this. Second, check your compiler for intrinsic functions implementing some form of SIMD instructions, such as Intel's SSE. For example Microsoft supplies a set. I believe SSE has some instructions for checking against a maximum value, and even clamping to the maximum if you want.

    0 讨论(0)
  • 2021-02-04 17:31

    You can't do anything faster than loops in just C++. You would need to use some platform specific vector instructions. That is, you would need to go down to the assembly language level. However, there are some C++ libraries that try to do this for you, so you can write at a high level and have the library take care of doing the low level SIMD work that is appropriate for whatever architecture you are targetting with your compiler.

    MacSTL is a library that you might want to look at. It was originally a Macintosh specific library, but it is cross platform now. See their home page for more info.

    0 讨论(0)
  • 2021-02-04 17:34

    Here is an SSE4 implementation that should perform pretty well on Nehalem (Core i7):

    #include <limits.h>
    #include <emmintrin.h>
    #include <smmintrin.h>
    
    static inline int canvas_add(int canvas[10][10], int addon[10][10])
    {
        __m128i * cp = (__m128i *)&canvas[0][0];
        const __m128i * ap = (__m128i *)&addon[0][0];
        const __m128i vlimit = _mm_set1_epi32(100);
        __m128i vmax = _mm_set1_epi32(INT_MIN);
        __m128i vcmp;
        int cmp;
        int i;
    
        for (i = 0; i < 10 * 10; i += 4)
        {
            __m128i vc = _mm_loadu_si128(cp);
            __m128i va = _mm_loadu_si128(ap);
    
            vc = _mm_add_epi32(vc, va);
            vmax = _mm_max_epi32(vmax, vc);   // SSE4 *
    
            _mm_storeu_si128(cp, vc);
    
            cp++;
            ap++;
        }
        vcmp = _mm_cmpgt_epi32(vmax, vlimit); // SSE4 *
        cmp = _mm_testz_si128(vcmp, vcmp);    // SSE4 *
        return cmp == 0;
    }
    

    Compile with gcc -msse4.1 ... or equivalent for your particular development environment.

    For older CPUs without SSE4 (and with much more expensive misaligned loads/stores) you'll need to (a) use a suitable combination of SSE2/SSE3 intrinsics to replace the SSE4 operations (marked with an * above) and ideally (b) make sure your data is 16-byte aligned and use aligned loads/stores (_mm_load_si128/_mm_store_si128) in place of _mm_loadu_si128/_mm_storeu_si128.

    0 讨论(0)
  • 2021-02-04 17:39

    Here is an alternative.

    If you are 100% certain that all your values are between 0 and 100, you could change your type from an int to a uint8_t. Then, you could add 4 elements together at once of them together using uint32_t without worrying about overflow.

    That is ...

    uint8_t  array1[10][10];
    uint8_t  array2[10][10];
    uint8_t  dest[10][10];
    uint32_t *pArr1 = (uint32_t *) &array1[0][0];
    uint32_t *pArr2 = (uint32_t *) &array2[0][0];
    uint32_t *pDest = (uint32_t *) &dest[0][0];
    
    int  i;
    
    for (i = 0; i < sizeof (dest) / sizeof (uint32_t); i++) {
        pDest[i] = pArr1[i] + pArr2[i];
    }
    

    It may not be the most elegant, but it could help keep you from going to architecture specific code. Additionally, if you were to do this, I would strongly recommend you comment what you are doing and why.

    0 讨论(0)
提交回复
热议问题