0

I have the following question, can you please help me with it:

I have the following arrays of integers (size 1024) and I trying to find common elements present in all the arrays (along with the position at which the common element was found):

Array1: 15, 89, 100, 167, 202, ...
Array2: 16, 89, 109, 178,179, 202, ...
Array3: 15, 89, 100, 178, 189, 202, ...
Array4: 17, 89, 109, 167, 178, 202, ...
Array5: 7,   89, 100, 178, 179, 180, 202, ...

Now the common elements along with their position in the respective arrays are:

Array1: 89(2), 202(5), ...
Array2: 89(2), 202(6), ...
Array3: 89(2), 202(6), ...
Array4: 89(2), 202(6), ...
Array5: 89(2), 202(7), ...

Is it possible to keep these arrays in L1 cache while the arrays are getting intersected. I have written a simple C++ code which pushes the common element and its position as an std::pair into an std::vector. Is this code correct to keep the elements in L1 cache or should I modify my code...if yes, then please suggest.

Jannat Arora
  • 2,759
  • 8
  • 44
  • 70

1 Answers1

0

Your data will stay in cache as long as the processor has a need for it or the processor needs to put other data into the cache.

My best advice is to keep the data close together and perform all your data accessing together. For example: input all data, process all data, output all data. A worse case is: input some data, process some data, output some data, repeat for all data.

You may want to use arrays instead of vectors, because vectors dynamically allocate memory and may allocate at different times. If the size of the arrays doesn't change, go with the arrays.

Edit 1:
Here are some good links describing cache optimizations:
CPU Cache Optimization
Effective Cache Memory usage

The first link has good diagrams explaining how cache works.

You should also search for "data cache optimization". Here are some more links:
Keeping arrays in L1 cache
Low level C language optimizations
Data Locality
EETimes - Optimizing for cache performance

Community
  • 1
  • 1
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Thanks a lot, can you please illustrate the best and worst case scenario with an example. I'll be really very thankful to you for the same – Jannat Arora May 31 '14 at 00:25