Thrust Device Vector iterator location - distance

i am trying to find the index/location of an iterator, i use the thrust::distance(). however, it returns weird value.
the vector size is 10. and when i use this method it returns value of "131" .
here is a fully working example.
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <iostream>
#include <iomanip>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <thrust/random.h>
#include <thrust/unique.h>
#include <thrust/reduce.h>
#include <thrust/iterator/constant_iterator.h>
using namespace std;
template <typename Vector>
void print_vector(const std::string& name, const Vector& v)
{
typedef typename Vector::value_type T;
std::cout << " " << std::setw(20) << name << " ";
thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, ""));
std::cout << std::endl;
}
int main()
{
thrust::device_vector<int> x;
x.push_back(1);
x.push_back(10);
x.push_back(1);
x.push_back(11);
x.push_back(1);
x.push_back(11);
thrust::device_vector<int> y(10);
print_vector("Original",x);
thrust::sort(x.begin(),x.end());
print_vector("sort",x);
thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end());
std::cout<<*it<<std::endl;
//int newsize=it-y.begin();
int newsize=thrust::distance(y.begin(),it);
cout<<"nsz:"<<newsize<<endl;
return 0;
}

The iterator it is established with respect to the vector x:
thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end());
^ ^
But you are asking for the distance from this iterator to the beginning of the vector y:
int newsize=thrust::distance(y.begin(),it);
^
That doesn't make sense. There is no defined relationship between it and the vector y.
If you ask for the distance to the beginning of vector x instead, you'll get more sensible results:
$ cat t1244.cu
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/reduce.h>
#include <thrust/extrema.h>
#include <iostream>
#include <iomanip>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <thrust/random.h>
#include <thrust/unique.h>
#include <thrust/reduce.h>
#include <thrust/iterator/constant_iterator.h>
using namespace std;
template <typename Vector>
void print_vector(const std::string& name, const Vector& v)
{
typedef typename Vector::value_type T;
std::cout << " " << std::setw(20) << name << " ";
thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout << std::endl;
}
int main()
{
thrust::device_vector<int> x;
x.push_back(1);
x.push_back(10);
x.push_back(1);
x.push_back(11);
x.push_back(1);
x.push_back(11);
thrust::device_vector<int> y(10);
print_vector("Original",x);
thrust::sort(x.begin(),x.end());
print_vector("sort",x);
thrust::device_vector<int>::iterator it=thrust::unique(x.begin(),x.end());
std::cout<<*it<<std::endl;
//int newsize=it-y.begin();
int newsize=thrust::distance(x.begin(),it);
cout<<"nsz:"<<newsize<<endl;
return 0;
}
$ nvcc -o t1244 t1244.cu
$ ./t1244
Original 1 10 1 11 1 11
sort 1 1 1 10 11 11
10
nsz:3
$

Related

Receiver not receiving data sent via segqueue

RECEIVER
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
/*
sigval_int
sigval_ptr
*/
void signal_handler(int signum, siginfo_t *siginfo, void *ucontext) {
printf("I got value %d\n", siginfo->si_int);
}
int main() {
pid_t pid = getpid();
struct sigaction act;
printf("My PID is %d\n", pid);
act.sa_sigaction = signal_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO | SA_RESTART;
sigaction(SIGUSR1, &act, NULL);
while (1);// sleep(20);
return EXIT_SUCCESS;
}
SENDER
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
int main(int argc, char *argv[]) {
pid_t pid;
union sigval sv;
if (argc < 2 || (pid = atoi(argv[1])) < 0) exit(EXIT_FAILURE);
sv.sival_int = 56;
printf("sender: sending %d to PID %d\n", sv.sival_int, pid);
sigqueue(pid, SIGUSR1, sv);
return EXIT_SUCCESS;
}
I am trying to run these two processes in two different terminals. First I ran the receiver to note down its PID. Next I used that pid to give command line arguments while executing sender.
The sender shows that it sent the data. But apparently the receiver did not get it. It was as if the signal handler was never invoked. Can someone help with some way to solve this.
TIA

why my code which includes hashfunction and unordered_set uncompilable?

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
struct hashfunction{
size_t operator()(pair<string,int> x){
size_t ret=5381;
for(int i=0;i<x.first.length();i++){
ret=(ret<<5)+(ret)+x.first[i];
}
ret+=x.second;
return ret;
}
};
unordered_set<pair<string,int>,hashfunction> myset;
int main(void)
{
string a="123";
int b= 4;
myset.insert({a,b});
}
I am studying unordered_set and hashfunction. But the code above is not compilable. why not? Anyone can explain why it is not compilable and how to solve this problem?
const is needed after operator()(pair<string,int> x).

how to call a function of c++ file in .m file?

I have a header file test.h
#ifndef __visibilty_test__test__
#define __visibilty_test__test__
#include <iostream>
using namespace std;
class test{
public:
void print(string s);
};
#endif
and test.mm
#include "test.h"
using namespace std;
void test:: print(string s){
cout << s << endl;
}
now I want to call print function in my AppDelegate.m file in an iOS Application. Can anyone help me?
Thanks in advance
Rename AppDelegate.m to AppDelegate.mm.
Call the method as you would in C++:
test t;
t.print("Hello");
Your object:
#include <iostream>
using namespace std;
class test
{
public:
void print(string s)
{
cout << s << endl;
}
};
call from mm file
test *t = new test;
t->print("hello");

C socket: Connection refused[Errno 111]

I'm writing a simple socket server/client. Here is the server part:
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
using namespace std;
int main() {
int listenfd;
int connfd;
sockaddr_in servaddr;
char buf[100];
time_t ticks;
if(listenfd = socket(AF_INET,SOCK_STREAM,0) < 0)
cout << "listenfd" << endl;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(10000);
bind(listenfd,(const struct sockaddr*)&servaddr,sizeof(servaddr));
listen(listenfd,5);
for(;;) {
connfd = accept(listenfd,(struct sockaddr *)NULL,NULL);
//cout << "accept link" << endl;
ticks = time(NULL);
snprintf(buf,sizeof(buf),"%.24s\r\n",ctime(&ticks));
//cout << buf << endl;
write(connfd,buf,strlen(buf));
close(connfd);
}
}
And here is the client part:
#include <netinet/in.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
using namespace std;
#define MAX 100
int main(int argc,char **argv) {
int socketfd;
int n;
char buf[MAX+1];
sockaddr_in servaddr;
if(argc !=2 )
cout << "stdin error " << endl;
if((socketfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
cout << " socekt error " << endl;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(10000);
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0)
cout << "inet_pton error" << endl;
cout << "prepare linking" << endl;
if(connect(socketfd,(const struct sockaddr*)&servaddr,sizeof(struct sockaddr)) < 0)
{
cout << " connet error" << endl;
cout << strerror(errno);
}
while((n = read(socketfd,buf,MAX)) >0) {
buf[n] = 0;
if(fputs(buf,stdout) == EOF)
cout << "cout error" << endl;
}
if(n < 0)
cout << "read error" << endl;
exit(0);
}
I start the server first and run the client like: ./client 127.0.0.1, but connection failed with errno 111.
I'm using Ubuntu 12.04 system.
In your server code, you have:
if(listenfd = socket(AF_INET,SOCK_STREAM,0) < 0)
The problem is to do with the precedence of C operators. Because the < comparison has a higher precedence than assignment, your statement will set listenfd to the boolean value of the x < y bit meaning, because the socket will most likely succeed, it will most likely be set to 0 (false), hence standard input (file descriptor 0).
If you must use the C shortcuts (I know they're handy but sometimes they're less readable than the alternatives), you should use the variant:
if ((listenfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
as you have already done in your client code:
if ((socketfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)

LLVM: "Export" class

I'd like to call this code from my program using LLVM:
#include <string>
#include <iostream>
extern "C" void hello() {
std::cout << "hello" << std::endl;
}
class Hello {
public:
Hello() {
std::cout <<"Hello::Hello()" << std::endl;
};
int hello() {
std::cout<< "Hello::hello()" << std::endl;
return 99;
};
};
I compiled this code to llvm byte code using clang++ -emit-llvm -c -o hello.bc hello.cpp and then I want to call it from this program:
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/IRReader.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
using namespace llvm;
void callFunction(string file, string function) {
InitializeNativeTarget();
LLVMContext context;
string error;
MemoryBuffer* buff = MemoryBuffer::getFile(file);
assert(buff);
Module* m = getLazyBitcodeModule(buff, context, &error);
ExecutionEngine* engine = ExecutionEngine::create(m);
Function* func = m->getFunction(function);
vector<GenericValue> args(0);
engine->runFunction(func, args);
func = m->getFunction("Hello::Hello");
engine->runFunction(func, args);
}
int main() {
callFunction("hello.bc", "hello");
}
(compiled using g++ -g main.cpp 'llvm-config --cppflags --ldflags --libs core jit native bitreader')
I can call the hello() function without any problems.
My question is: how can I create a new instance of the Hello class using LLVM?
I'm getting a segmentation fault when I call Hello::Hello()
Thanks for any hints!!
Manuel
Running clang++ -emit-llvm on the given source won't emit Hello::Hello, and m->getFunction("Hello::Hello") wouldn't find it even if it were emitted. I would guess it's crashing because func is null.
Trying to directly call functions which aren't extern "C" from the LLVM JIT is generally not recommended... I'd suggest writing a wrapper like the following, and compiling it with clang (or using the clang API, depending on what you're doing):
extern "C" Hello* Hello_construct() {
return new Hello;
}