Skip to content

Commit

Permalink
add MPI::Comm#Alltoall method
Browse files Browse the repository at this point in the history
  • Loading branch information
seiya committed Apr 21, 2011
1 parent 7ecdfc6 commit 5ee7294
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ext/mpi/mpi.c
Expand Up @@ -374,6 +374,26 @@ rb_comm_scatter(VALUE self, VALUE rb_sendbuf, VALUE rb_recvbuf, VALUE rb_root)
return Qnil;
}
static VALUE
rb_comm_alltoall(VALUE self, VALUE rb_sendbuf, VALUE rb_recvbuf)
{
void *sendbuf, *recvbuf;
int sendcount, recvcount;
MPI_Datatype sendtype, recvtype;
int rank, size;
struct _Comm *comm;
OBJ2C(rb_sendbuf, sendcount, sendbuf, sendtype);
Data_Get_Struct(self, struct _Comm, comm);
check_error(MPI_Comm_rank(comm->comm, &rank));
check_error(MPI_Comm_size(comm->comm, &size));
OBJ2C(rb_recvbuf, recvcount, recvbuf, recvtype);
if (recvcount < sendcount)
rb_raise(rb_eArgError, "recvbuf is too small");
recvcount = recvcount/size;
sendcount = sendcount/size;
check_error(MPI_Alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm->comm));
return Qnil;
}
static VALUE
rb_comm_get_Errhandler(VALUE self)
{
struct _Comm *comm;
Expand Down Expand Up @@ -471,6 +491,7 @@ void Init_mpi()
rb_define_method(cComm, "Gather", rb_comm_gather, 3);
rb_define_method(cComm, "Allgather", rb_comm_allgather, 2);
rb_define_method(cComm, "Scatter", rb_comm_scatter, 3);
rb_define_method(cComm, "Alltoall", rb_comm_alltoall, 2);
rb_define_method(cComm, "Errhandler", rb_comm_get_Errhandler, 0);
rb_define_method(cComm, "Errhandler=", rb_comm_set_Errhandler, 1);

Expand Down
14 changes: 14 additions & 0 deletions spec/ruby-mpi_spec.rb
Expand Up @@ -125,6 +125,20 @@
recvbuf.should eql(rank.to_s*bufsize)
end

it "should change data between each others" do
world = MPI::Comm::WORLD
rank = world.rank
size = world.size
bufsize = 2
sendbuf = rank.to_s*bufsize*size
recvbuf = "?"*bufsize*size
world.Alltoall(sendbuf, recvbuf)
str = ""
size.times{|i| str << i.to_s*bufsize}
recvbuf.should eql(str)
end




it "shoud raise exeption" do
Expand Down

0 comments on commit 5ee7294

Please sign in to comment.