Skip to content

Commit

Permalink
info: add MPIX_Info_set_hex
Browse files Browse the repository at this point in the history
Add MPIX_Info_set_hex to allow user to provide info hints with binary
values. This routine provide a consistent way of encoding binary values
into string that implementation can decode.
  • Loading branch information
hzhou committed Mar 31, 2022
1 parent 3f4dc81 commit 2501347
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 49 deletions.
6 changes: 6 additions & 0 deletions src/binding/c/stream_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ MPIX_Recv_enqueue:

MPIX_Stream_synchronize:
stream: STREAM, [stream object]

MPIX_Info_set_hex:
info: INFO, direction=in, [info object]
key: STRING, constant=True, [key]
value: BUFFER, constant=True, [value]
value_size: INFO_VALUE_LENGTH, [size of value]
97 changes: 64 additions & 33 deletions src/mpi/stream/stream_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,7 @@ int MPIR_Stream_free_impl(MPIR_Stream * stream_ptr)
*
* Returns MPICH error codes */

static int hex_val(char c)
{
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else {
return -1;
}
}

static int hex_decode(const char *str, void *buf, int len)
{
int n = strlen(str);
if (n != len * 2) {
return 1;
}

unsigned char *s = buf;
for (int i = 0; i < len; i++) {
int a = hex_val(str[i * 2]);
int b = hex_val(str[i * 2 + 1]);
if (a < 0 || b < 0) {
return 1;
}
s[i] = (unsigned char) ((a << 4) + b);
}

return 0;
}

static int hex_decode(const char *str, void *buf, int len);
int MPIR_Stream_create_impl(MPIR_Info * info_ptr, MPIR_Stream ** p_stream_ptr)
{
int mpi_errno = MPI_SUCCESS;
Expand Down Expand Up @@ -341,3 +309,66 @@ int MPIR_Stream_synchronize_impl(MPIR_Stream * stream_ptr)

return mpi_errno;
}

static int hex_encode(char *str, const void *value, int len);
int MPIR_Info_set_hex_impl(MPIR_Info * info_ptr, const char *key, const void *value, int value_size)
{
int mpi_errno = MPI_SUCCESS;

char value_buf[1024];
MPIR_Assertp(value_size * 2 + 1 < 1024);

hex_encode(value_buf, value, value_size);

mpi_errno = MPIR_Info_set_impl(info_ptr, key, value_buf);

return mpi_errno;
}

/* ---- internal utility ---- */

static int hex_val(char c)
{
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else {
return -1;
}
}

static int hex_encode(char *str, const void *value, int len)
{
/* assume the size of str is already validated */

const unsigned char *s = value;

for (int i = 0; i < len; i++) {
sprintf(str + i * 2, "%02x", s[i]);
}

return 0;
}

static int hex_decode(const char *str, void *buf, int len)
{
int n = strlen(str);
if (n != len * 2) {
return 1;
}

unsigned char *s = buf;
for (int i = 0; i < len; i++) {
int a = hex_val(str[i * 2]);
int b = hex_val(str[i * 2 + 1]);
if (a < 0 || b < 0) {
return 1;
}
s[i] = (unsigned char) ((a << 4) + b);
}

return 0;
}
17 changes: 1 addition & 16 deletions test/mpi/impls/mpich/cuda/stream.cu
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@ void saxpy(int n, float a, float *x, float *y)
if (i < n) y[i] = a*x[i] + y[i];
}

static void hex_encode(char *buf, int len, cudaStream_t stream)
{
int n = sizeof(cudaStream_t);
assert(len >= n * 2 + 1);

unsigned char *s = (unsigned char *) &stream;
for (int i = 0; i < n; i++) {
sprintf(buf + i * 2, "%02x", s[i]);
}
}

int main(void)
{
int errs = 0;
Expand Down Expand Up @@ -89,14 +78,10 @@ int main(void)
init_y(y);
}

/* hexadecimal encoding */
char str_stream[100];
hex_encode(str_stream, 100, stream);

MPI_Info info;
MPI_Info_create(&info);
MPI_Info_set(info, "type", "cudaStream_t");
MPI_Info_set(info, "id", str_stream);
MPIX_Info_set_hex(info, "id", &stream, sizeof(stream));

MPIX_Stream mpi_stream;
MPIX_Stream_create(info, &mpi_stream);
Expand Down

0 comments on commit 2501347

Please sign in to comment.