atomicAdd - CUDA function - cuda

I apply atomicAdd function to add 10 in each array component
The results are Not identical to my expection.
Could you tell me why the value of list[1] is 12, while I expect 11=1+10. Total threads are 5. The initial array values are
slist[0]=1
slist[1]=2
slist[2]=3
slist[3]=4
slist[4]=5
the results are
list[0]= 1, list[0]= 1
list[0]= 1, list[1]= 12
list[0]= 1, list[2]= 13
list[0]= 1, list[3]= 14
list[0]= 1, list[4]= 15
__global__ void RunAtomicAdd(int* slist, int* val)
{
int id = threadIdx.x;
slist[0] = atomicAdd((slist +id), 10);
printf("list[0]= %d, list[%d]= %d \n", slist[0], id, slist[id]);
}

Note that atomicAdd does not return the updated value, instead it returns the old value: cuda atomicAdd example fails to yield correct output
So all of your outputs are expected. In slist[0], even if you update the value with atomicAdd, you immediately overwrite it with the output of atomicAdd, the old value. This does not happen with the rest of the id, except they do indeed store 1 in slist[0], all of them.
You may want to have a new array to store the result of atomicAdd.

Related

How to check efficiently if in number binary representation all next bit is different?

I need to write the function to check if the number binary representation doesn't contain duplications. For example, the function must return true if N equals to 42, because bin(42) equals to 101010, but if N equals to 45 the function must return false, because of binary representation of 45 which equals to 101101 and which contains duplicates 11.
This allows only bits that alternate between 0 and 1, plus possibly leading zeros.
One way to check this is to look at (N << 2) | N. If N is of the correct form, then this is equal to N << 2, except for bit 0 or 1. We can compensate for that as follows:
unsigned N2 = N << 2;
return (N | N2) <= (N2 | 2);

Arduino - waiting on function causes a number pile-up

I have a function filledFunction() that returns a float filled:
float filledFunction(){
if (FreqMeasure.available()) {
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
frequency = FreqMeasure.countToFrequency(sum / count);
a = frequency * x;
b = exp (a);
c = w * b;
d = frequency * z;
e = exp (d);
f = y * e;
float filled = c + f;
sum = 0;
count = 0;
return filled;
}
}
}
When I call this function with
while (1){
fillLevel = filledFunction();
int tofill = 500 - fillLevel;
Serial.print("fillLevel: ");
Serial.println(fillLevel);
Serial.print("tofill: ");
Serial.println(tofill);
The serial monitor should output two numbers that add up to 500 named fillLevel and tofill. Instead I get a repeating sequence of similar values:
http://i.imgur.com/Y9Wu8P2.png
The First two values are correct (410.93 + 89 = 500), but the following 60ish values are unknown to me, and do not belong there.
I am using an arduino nano
The filledFunction() function only returns a value if FreqMeasure.available() returns true AND count > 30. As stated in the answers to this question the C89, C99 and C11 standards all say that the default return value of a function is undefined (that is if the function completes without executing a return statement). Which really means that anything could happen, such as outputting arbitrary numbers.
In addition, the output that you're seeing starts off 'correct' with one of the numbers subtracted from 500, even when they have weird values such as 11699.00 and -11199 (which equals 500 - 11699.00). However, lower down in the output this seems to break down and the reason is that on Arduino Nano an int can only hold numbers up to 32767 and therefore the result of the subtraction is too big and 'overflows' to be a large negative number.
Fixing the filledFunction() function to explicitly return a value even if FreqMeasure.available() is false or count <= 30 and ensuring that it can't return a number greater than 500 will likely solve these issues.

Cast number of bytes from blob field to number

I have a table with one blob field named bindata. bindata always contains 7 bytes. First four of them is an integer (unsigned I think, db is not mine).
My question is how can I select only the first four bytes from bindata and convert them to a number?
I am new in mySQL but from the documentation I see that I may have to use the conv function by doing something like this:
SELECT CONV(<Hex String of first 4 bytes of bindata>,16,10) as myNumber
But I don't have a clue on how to select only the first four bytes of the blob field. I am really stuck here.
Thanks
You can use string function to get partial of byte in the blob. For example:
SELECT id,
((ORD(SUBSTR(`data`, 1, 1)) << 24) +
(ORD(SUBSTR(`data`, 2, 1)) << 16) +
(ORD(SUBSTR(`data`, 3, 1)) << 8) +
ORD(SUBSTR(`data`, 4, 1))) AS num
FROM test;
Here is Demo in SQLFiddle

interpreting 1 bit counting

Write a function named bitCount() in bitcount.c that returns the number of 1-bits in the
binary representation of its unsigned integer argument. Remember to fill in the identification
information and run the completed program to verify correctness.
/*
Name:
Lab section time:
*/
#include <stdio.h>
int bitCount (unsigned int n);
int main ( ) {
printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n",
0, bitCount (0));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
1, bitCount (1));
printf ("# 1-bits in base 2 representation of %u = %d, should be 16\n",
2863311530u, bitCount (2863311530u));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
536870912, bitCount (536870912));
printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n",
4294967295u, bitCount (4294967295u));
return 0;
}
int bitCount (unsigned int n) {
/* your code here */
}
Can someone help me understand exactly what's that asking? Is bitCount supposed to convert the decimal inputted into binary, and then count the number of 1's?
There is no "decimal" input to the function, it's being passed plain (unsigned, it seems) numbers. They will be stored in binary on most typical computers.
I guess it would return these values, for instance:
bitcount(0) -> 0 (since 0 has no bits set)
bitcount(1) -> 1 (since 1 is 12 in binary)
bitcount(2) -> 1 (since 2 is 102 in binary)
bitcount(3) -> 2 (since 3 is 112 in binary)
It doesn't matter what base the number is given in in the source code where the function is called, that'll be converted to binary once the program runs. You can call it like bitcount(01) (octal) or bitcount(0x80), it's still just getting an unsigned int whose value can be assumed to be stored in binary.
A recursive algorithm for bitcount(x) is as such:
if x is 0, return 0
if x is 1, return 1
return bitcount(x mod 2) + bitcount(x / 2)
Note that the pseudocode doesn't assume that the number x is stored in any particular way (be it binary or whatever), it works on the number itself. The literals in the pseudocode are in decimal, but that's just notation.

CUDA thrust: how to realize "partition" that supports "stencil"?

Suppose there is an array of intergers:
A[]={2, 2, 9, 8, 5, 7, 0, 6}
and a stencil:
B[]={1, 0, 0, 1, 1, 1, 0, 1}
My question is how could we rearrange A[] according to B[] such that if B[i]==1, B[j]==0, then A[i] will be guaranteed to precede A[j] in the new array, which should look like:
C[]={2, 8, 5, 7, 6, 2, 9, 0}
PS: I found the "partition" function was almost the answer except that it only supported predicate. Is there any workaround?
Any hint is much appreciated!
This can be implemented using thrust::stable_sort_by_key().
Now that thrust::partition and thrust::stable_partition with stencil have been implemented (one may need to get the source from the official Thrust repository), this can be achieved with:
#include <thrust/partition.h>
struct is_one
{
__host__ __device__
bool operator()(const int &x)
{
return x == 1;
}
};
// Partition values on device thanks to stencil
thrust::stable_partition(d_A.begin(),
d_A.end(),
d_B.begin(),
is_one());
Which leads to:
A = 0 1 2 3 4 5 6 7 8 9
B = 0 1 1 0 0 1 0 0 1 0
C = 1 2 5 8 0 3 4 6 7 9
This implementation is more efficient since we are not sorting the values in the two partitions. A similar and more complex example is available here (with some more details in the answer).