简体   繁体   中英

Compare and Swap on a 2D Array

I'm trying to write to a 2d array (float**W) atomically from different threads. However CAS always gives this error: incompatible type for argument 1 of __sync_bool_compare_and_swap

c = __sync_bool_compare_and_swap(&W[uu][i], a, b);

It works fine as usual when I write atomcially to a 1d array.

Any ideas on how to make this work? I can try making 1d arrays in each thread and then updating this 2d array after a barrier, but that would take up too much memory. I'm using Ubuntu/Linux.

Thanks.

main() {
  int* W = malloc(10);  
  int uu = 1, i = 3;
  __sync_val_compare_and_swap(&W[uu], 1, 2);
}

Compiles fine, however:

main() {
  float* W = malloc(10);  
  int uu = 1, i = 3;
  __sync_val_compare_and_swap(&W[uu], 1.0f, 2.0f);
}

Does not compile giving me the exact same message you wrote. Which suggests that floats are not supported:

The definition given in the Intel documentation allows only for the use of the types int, long, long long as well as their unsigned counterparts. GCC will allow any integral scalar or pointer type that is 1, 2, 4 or 8 bytes in length.

and it looks like this confirms that.

If you're not using itanium then maybe

The four non-arithmetic functions (load, store, exchange, and compare_exchange) all have a generic version as well. This generic version works on any data type.

you might use __atomic_compare_exchange* because these should work on any types according to the docs. I haven't tried that yet though.

Edit:

main() {
  float* W = malloc(10);  
  float target;
  float val = 5.0f;
  __atomic_exchange(&W[4], &val, &target, __ATOMIC_RELAXED);
}

^- this at least compiles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM