Opencl- Relational functions - opencl

I have tried to use relational functions in my program but something is weird.
Some vector A has each value;
If I try print this vector A, it works properly.
However if I add some relational functions in my code for vector A,
It returns all zero;
If one of vector element has NAN value, does that make all-zero?
What is wrong? Please help.
//////////////////////////////////////
i tried simple example code then it looks like work in wrong way.
host code
float *array;
array = (float *) malloc(sizeof(float) * 16);
array[0] = 1;
array[1] = 0.5;
array[2] = 0;
array[3] = -0.5;
array[4] = -1;
array[5] = 1;
array[6] = 0.5;
array[7] = 0;
array[8] = -0.5;
array[9] = -1;
array[10] = 1;
array[11] = 0.5;
array[12] = 0;
array[13] = -0.5;
array[14] = -1;
array[15] = -1;
int *array2;
array2 = (int *) malloc(sizeof(int) * 16);
test_buffer1 = cl::Buffer(gContext,
CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR, 16 * sizeof(cl_float), &array,
NULL);
test_buffer2 = cl::Buffer(gContext,
CL_MEM_READ_WRITE , 16 * sizeof(cl_int), NULL,
NULL);
test_func.setArg(0, test_buffer1);
test_func.setArg(1, test_buffer2);
gQueue.enqueueNDRangeKernel(test_func, cl::NDRange(0, 0),
cl::NDRange(1, 1), cl::NDRange(1, 1), NULL, NULL);
gQueue.enqueueReadBuffer(test_buffer2, CL_TRUE, 0,
16 * sizeof(cl_int), array2);
LOGV("----test arr----");
LOGV("element 0 : %d", array2[0]);
LOGV("element 1 : %d", array2[1]);
LOGV("element 2 : %d", array2[2]);
LOGV("element 3 : %d", array2[3]);
LOGV("element 4 : %d", array2[4]);
LOGV("element 5 : %d", array2[5]);
LOGV("element 6 : %d", array2[6]);
LOGV("element 7 : %d", array2[7]);
LOGV("element 8 : %d", array2[8]);
LOGV("element 9 : %d", array2[9]);
LOGV("element 10 : %d", array2[10]);
LOGV("element 11: %d", array2[11]);
LOGV("element 12: %d", array2[12]);
LOGV("element 13: %d", array2[13]);
LOGV("element 14: %d", array2[14]);
LOGV("element 15: %d", array2[15]);
LOGV("----test end----");
kerenel code
__kernel void Test_func1(
__global float* datatin,
__global int* dataout)
{
float16 inputdata;
int16 iszero;
float16 result;
inputdata=vload16(0,datatin);
iszero=isequal(inputdata,(float16)0);
// result=convert_float16(iszero);
vstore16(iszero,0,dataout);
}
result of above code
08-19 01:30:06.064: V/FBIA_JNI(17250): element 0 : 0
08-19 01:30:06.064: V/FBIA_JNI(17250): element 1 : -1
08-19 01:30:06.064: V/FBIA_JNI(17250): element 2 : 0
08-19 01:30:06.064: V/FBIA_JNI(17250): element 3 : -1
08-19 01:30:06.064: V/FBIA_JNI(17250): element 4 : -1
08-19 01:30:06.064: V/FBIA_JNI(17250): element 5 : -1
08-19 01:30:06.064: V/FBIA_JNI(17250): element 6 : -1
08-19 01:30:06.064: V/FBIA_JNI(17250): element 7 : 0
08-19 01:30:06.064: V/FBIA_JNI(17250): element 8 : 0
08-19 01:30:06.064: V/FBIA_JNI(17250): element 9 : 0
08-19 01:30:06.064: V/FBIA_JNI(17250): element 10 : 0
08-19 01:30:06.064: V/FBIA_JNI(17250): element 11: 0
08-19 01:30:06.064: V/FBIA_JNI(17250): element 12: -1
08-19 01:30:06.064: V/FBIA_JNI(17250): element 13: -1
08-19 01:30:06.064: V/FBIA_JNI(17250): element 14: 0
08-19 01:30:06.064: V/FBIA_JNI(17250): element 15: -1

Related

Papyrus - OCL constraint to verified property

My class has 2 properties (String) that are two types of people's document numbers. To verify if the documents has valid numbers, a calculation is realized (verifiers digits). Below an example how is realized a consistensy of one of them:
Number: 973.345.650-02 (The punctuations must be ignored)
FIRST VERIFIER DIGIT CALCULATION
9 * 10 = 90
7 * 9 = 63
3 * 8 = 24
3 * 7 = 21
4 * 6 = 24
5 * 5 = 25
6 * 4 = 24
5 * 3 = 15
0 * 2 = 0
----------
Sum = 286
286 % 11 = 0
If rest < 2 then first digit = 0
Or if rest >= 2 then first digit = 11 - rest
In this case, rest < 2 (0), then first verifier digit = 0
SECOND VERIFIER DIGIT CALCULATION
9 * 11 = 99
7 * 10 = 70
3 * 9 = 27
3 * 8 = 24
4 * 7 = 28
5 * 6 = 30
6 * 5 = 30
5 * 4 = 20
0 * 3 = 0
0 * 2 = 0 ==> FIRST VERIFIER DIGIT
----------
Sum = 328
328 % 11 = 9
Same rule of first verifier digit
If rest < 2 then first digit = 0
Or if rest >= 2 then first digit = 11 - rest
The rest is greater than 2 (9), then second verifiter digit = 11 - 9 ==> 2
JAVA METHOD
public static boolean isValidCPF(String cpf) {
cpf = cpf.replaceAll("[./-]", "");
if (cpf.length() < 11) {
return false;
}
int equalDigits = 0;
char compareChar = cpf.charAt(0);
for (int i = 1; i <= 9; i++) {
if (compareChar == cpf.charAt(i)) {
equalDigits++;
} else {
break;
}
}
if (equalDigits == 9) {
return false;
}
int[] digit = new int[2];
int sum = 0, multiply = 2;
for (int k = 8; k <= 9; k++) {
for (int i = k; i >= 0; i--) {
sum += Character.getNumericValue(cpf.charAt(i)) * multiply++ ;
}
digit[k-8] = (sum % 11) < 2 ? 0 : 11 - (sum % 11);
sum = 0;
multiply = 2;
}
if (cpf.equals(cpf.substring(0 , 9) + digit[0] + digit[1])) {
return true;
}
return false;
}
I guess that OCL "sequence" must be used in this case (loop through digits) converting each one to Integer for calculation and using "body" in constraint, but I don't know how.
I want to apply the contraint to UML model in Papyrus (that's I know how to do).
Thanks in advance.
You must think declaratively in aggregates so to emulate
for (int i = 1; i <= 9; i++) {
if (compareChar == cpf.charAt(i)) {
equalDigits++;
} else {
break;
}
}
you might try something like
Sequence{2..10}->select(i | cpf->at(i) = compareChar)->size()
NB OCL indexes start at 1.

Standard Positional to Bijective Base Conversion

We know that a b-based standard positional number system uses digits,
0, 1, 2, ..., b-1. But a bijective number system uses digits, 1, 2, ..., b. So a 4-based standard number system sequence looks like,
0
1
2
3
10
11
12
13
20
21
22
23
30
31
32
33 (base-4, 16th number standard)
100 (base-4, 17th number standard)
101
.
.
.
On the other hand bijective number system for 4-based looks like,
λ (base-4, 1st number, empty-string)
1
2
3
4
11
12
13
14
21
22
23
24
31
32
33 (base-4, 16th number bijective)
34 (base-4, 17th number bijective)
41
.
.
.
Example:
34152 (in bijective base-5) = 3×54 + 4×53 + 1×52 + 5×51 + 2×1 = 2427 (in decimal).
119A (in bijective base-10, with "A" representing the digit value ten) = 1×103 + 1×102 + 9×101 + 10×1 = 1200 (in decimal).
I wonder if there is any easy way to find n'th position bijective value in same base.
For example,
lets say in base-4 5th positional value = 10 (standard) but 5th positional value = 11 (bijective). Any pseudocode is ok to understand the concept.
If a number in the standard base-n system contains no zeros, the number has the same representation as bijective base-n.
So you need to look for zeros in the standard number. When you find a zero, replace it with the maximum symbol in the bijective and decrement the element to the left.
Simple example:
10 decimal -> A bijective because 0 becomes A and 1 decrements to zero
20 decimal -> 1A bijective because 0 becomes A and 2 decrements to 1
As a special case you must handle sequences of zero.
Simple example:
200 decimal -> 19A bijective (i.e. step 1: 1A0 step 2: 19A)
You can also look at it like this
200 decimal is constructed as 2*100 + 0*10 + 0*1 = 200 decimal
For bijective you can't have zero so you'll do:
19A bijective is constructed as 1*100 + 9*10 + 10*1 = 200 decimal
To finish a bit more complex example:
110 decimal -> AA bijective (i.e. step 1: 10A step 2: AA)
This routine implements the conversion. (Which resolved to #4386427's method.) If you want the other version, where 100 (base 4 std) -> 41 (base 4 bij') then compile with -D NO_EMPTY_STRING
#include <stdio.h>
#include <string.h>
void print_be_digits(const char *prefix, const unsigned char *le_num, size_t len)
{
size_t i;
printf("%s", prefix);
for(i=0; i<len; i++)
{
printf("%d ", (int)le_num[len-i-1]);
}
printf("\n");
}
void nth_bij(int n, int k)
{
ssize_t i;
size_t std_len;
size_t bij_len;
size_t work;
unsigned char le_std_digits[256];
unsigned char le_bij_digits[256];
//convert to standard radix-k digits
work = n;
for(std_len = 0; work; std_len++)
{
le_std_digits[std_len] = work % k;
work /= k;
}
print_be_digits(" std: ", le_std_digits, std_len);
//convert standard to bij
memcpy(le_bij_digits, le_std_digits, std_len);
bij_len = std_len;
#ifdef NO_EMPTY_STRING
// Step 1: increment LSd
le_bij_digits[0]++;
#endif
// Step 2: borrow on zeros
// scan back from the end
for(i=bij_len-1; i>= 0; i--)
{
//if we find a zero, borrow, and ripple toward MSd as necessary
if(le_bij_digits[i] == 0)
{
size_t j;
//Ripple borrow toward MSd, as necessary
for(j=i+1; j<bij_len; j++)
{
le_bij_digits[j-1] = k; //k is the radix
if(--le_bij_digits[j])
{
break;
}
}//end ripple
//adjust bij_len if we rippled to the end
if(j == bij_len)
{
bij_len--;
}
}
}//end scan
print_be_digits(" bij: ", le_bij_digits, bij_len);
}
Simple driver:
int main(int argc, char *argv[])
{
printf("Test: 16 decimal (->base 4): \n");
nth_bij(16,4);
printf("\n");
printf("Test: 8 decimal (->base 2): \n");
nth_bij(8,2);
printf("\n");
printf("Test: 13 decimal (->base 2): \n");
nth_bij(13,2);
printf("\n");
printf("Test: 2427 decimal (->base 5): \n");
nth_bij(2427, 5);
printf("\n");
printf("Test: 1200 decimal (->base 10): \n");
nth_bij(1200, 10);
printf("\n");
}
Compiling for my version:
$ gcc -D NO_EMPTY_STRING bij.c
$ ./a.exe
Test: 16 decimal (->base 4):
std: 1 0 0
bij: 4 1
Test: 8 decimal (->base 2):
std: 1 0 0 0
bij: 1 2 1
Test: 13 decimal (->base 2):
std: 1 1 0 1
bij: 2 2 2
Test: 2427 decimal (->base 5):
std: 3 4 2 0 2
bij: 3 4 1 5 3
Test: 1200 decimal (->base 10):
std: 1 2 0 0
bij: 1 1 10 1
Compiling for #4386427's version:
$ gcc bij.c
$ ./a.exe
Test: 16 decimal (->base 4):
std: 1 0 0
bij: 3 4
Test: 8 decimal (->base 2):
std: 1 0 0 0
bij: 1 1 2
Test: 13 decimal (->base 2):
std: 1 1 0 1
bij: 2 2 1
Test: 2427 decimal (->base 5):
std: 3 4 2 0 2
bij: 3 4 1 5 2
Test: 1200 decimal (->base 10):
std: 1 2 0 0
bij: 1 1 9 10

Why are my identical go routines out of order?

I've got the following go code executing routines.
package main
import (
"fmt"
"time"
)
func count(id int) {
for i := 0; i < 10; i++ {
fmt.Println(id, ":", i)
time.Sleep(time.Millisecond * 1000)
}
}
func main() {
for i := 0; i < 10; i++ {
go count(i)
}
time.Sleep(time.Millisecond * 11000)
}
I would expect the output to be:
1 : 0
2 : 0
3 : 0
4 : 0
5 : 0
6 : 0
7 : 0
8 : 0
9 : 0
1 : 1
2 : 1
3 : 1
4 : 1
etc...
but instead, I get:
0 : 0
6 : 0
7 : 0
5 : 0
8 : 0
9 : 0
3 : 0
2 : 0
4 : 0
1 : 0
5 : 1
6 : 1
7 : 1
1 : 1
8 : 1
etc...
Why are they not in order of the original, outside for loop executing the count method? Why are some count methods getting out of sync?
When a goroutine executes is out of the control of the programmer. You have some control if you use signals via channels and sync.WaitGroup, (like having one goroutine wait until another has finished) but you cannot control the execution order of goroutines
As others already commented, goroutine is mainly aiming to concurrency. Concurrency can embrace parallelism and communication. If you want to control their order, you have to make them communicate through SIGNAL. For example, a goroutine waits for a signal from another one. Please check goroutine and channel in particular.
example: https://micknelson.wordpress.com/2012/11/14/a-tour-of-go-the-web-crawler-exercise/
packages : https://golang.org/pkg/os/signal/

Google analytics tracking page visits or state

Code example
$(function () {
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-[Site]-[Nr]', [SiteLabel]);
ga('set', {
'dimension1': [State],
'dimension2': [Title],
'dimension3': [LocationID],
'dimension4': [Owner],
'dimension5': [UserID],
'dimension6': [Uri],
'dimension7': [Exception]
});
ga('send', 'pageview');
});
Use case example
Use cases where [A, B, C, D] are different pages and S* is the session.
#Use case 1
------------------
S1 : A B A B
S2 : A B A
S3 : B
#Use case 2
------------------
S1 : A B A C A D A
#Use case 3
------------------
S1 : A B C D
#Use case 4
------------------
S1 : A A A A A
Expected results:
Mode 1: Hit – value is applied to the single hit for which it has been set.
Mode 2: Session – value is applied to all hits in a single session.
#Use case 1
Mode 1:
A : 4 visits
B : 4 visits
Mode 2:
A : 2 visits
B : 3 visits
#Use case 2
Mode 1:
A : 4 visits
B : 1 visits
C : 1 visits
D : 1 visits
Mode 2:
A : 1 visits
B : 1 visits
C : 1 visits
D : 1 visits
#Use case 3
Mode 1:
A : 1 visits
B : 1 visits
C : 1 visits
D : 1 visits
Mode 2:
A : 1 visits
B : 1 visits
C : 1 visits
D : 1 visits
#Use case 4
Mode 1:
A : 5 visits
Mode 2:
A : 1 visits
Results I seem to get
Changing the scope does not seem to have any effect.
#Use case 1
A : 2 (7 page visits)
B : 1 (1 page visits)
#Use case 2
A : 1 (7 page visits)
#Use case 3
A : 1 (4 page visits)
#Use case 4
A : 1 (5 page visits)
Question
How can I get expected results?
First since you are setting custom dimensions I believe [A, B, C, D] should represent different values of custom dimensions, not pages.
When using Hit Level Custom Dimensions you should be looking at pageviews instead of visits/sessions. So what you really should be expecting to see is:
#Use case 1
Mode 1:
A : 4 pageviews
B : 4 pageviews
Mode 2:
A : 2 visits
B : 3 visits
#Use case 2
Mode 1:
A : 4 pageviews
B : 1 pageviews
C : 1 pageviews
D : 1 pageviews
Mode 2:
A : 1 visits
B : 1 visits
C : 1 visits
D : 1 visits
#Use case 3
Mode 1:
A : 1 pageviews
B : 1 pageviews
C : 1 pageviews
D : 1 pageviews
Mode 2:
A : 1 visits
B : 1 visits
C : 1 visits
D : 1 visits
#Use case 4
Mode 1:
A : 5 pageviews
Mode 2:
A : 1 visits
If you redo your tests with this in mind the numbers should match.

MPI: all to all broadcast on one dimensional array

I have number of processors and one array, and each processor fills their work into, one dimensional array like:
dense_process_array for process 1:
|process1|0000000000000|
dense_process_array for process 2:
|000000|process2|000000|
each process fills their interval, then I want to all processors have others results into same array.
Process 1 => dense_process_array |process1|process2|.....|processN|
Process 2 ... Process N
(like all2all bcast) Therefore, Every process calls this function:
void doCommunication(int id, int numprocs, int start_point, int end_point) {
int size_of_length = end_point - start_point + 1;
MPI_Scatter(dense_process_array+start_point, size_of_length, MPI_DOUBLE, dense _process_array +start_point, size_of_length, MPI_DOUBLE, id, MPI_COMM_WORLD;
}
But, in the end when I looked my array from any process, I seen that it can not get results of other processes, can you suggest anything?
not: I'm new in MPI, and basicly I want to all2all bcast.
I believe you're looking for MPI_Allgather:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
void printdata(int size, int rank, int n, int *data) {
printf("Rank %d\n",rank);
for (int j=0; j<size*n; j++)
printf("%d ",data[j]);
printf("\n");
}
int main(int argc, char **argv) {
const int n=3;
int ierr, rank, size;
int *datain, *dataout;
ierr = MPI_Init(&argc, &argv);
ierr|= MPI_Comm_size(MPI_COMM_WORLD,&size);
ierr|= MPI_Comm_rank(MPI_COMM_WORLD,&rank);
datain = (int *)malloc(n*size*sizeof(int));
dataout = (int *)malloc(n*size*sizeof(int));
for (int i=0; i<n*size; i++)
datain[i]=9;
for (int i=0; i<n; i++)
datain[rank*n+i]=rank;
if (rank == 0) printf("Before:\n");
printdata(size, rank, n, datain);
MPI_Allgather(&(datain[rank*n]), n, MPI_INT, dataout, n, MPI_INT, MPI_COMM_WORLD);
if (rank == 0) printf("After:\n");
printdata(size, rank, n, dataout);
free(datain);
free(dataout);
MPI_Finalize();
return 0;
}
Running gives
$ mpirun -np 3 ./allgather
Before:
Rank 0
0 0 0 9 9 9 9 9 9
Rank 1
9 9 9 1 1 1 9 9 9
Rank 2
9 9 9 9 9 9 2 2 2
After:
Rank 0
0 0 0 1 1 1 2 2 2
Rank 1
0 0 0 1 1 1 2 2 2
Rank 2
0 0 0 1 1 1 2 2 2

Resources