1
votes

So I have to process some struct data which I've had to compress in a weird way for MPI_Send/Recv.

There's no way to comfortably cast it to a pointer to a known type on which I want to operate and then index along (since I've squeezed the struct into contiguous data, can't assume anything about alignment), so I have to go through it with pointer arithmetic. Problem is, to keep consistent with MPI, this data is given as void*, and pointer arithmetic on void* is illegal.

My question is mostly stylistic: is there a better way to do this than to cast to char* and then do my pointer arithmetic? What would be the efficiency considerations in taking a void*, casting to char*, doing my stuff to it, then casting back to void*? I can't imagine that pointer casts would be terribly expensive.

G'day and thankee much.

3

3 Answers

3
votes

Pointer casts are completely free. Do it.

A pointer having a type is an entirely language level notion. When you get down to the hardware level a pointer is just an integer that happens to be used as a memory address.

You do have to be careful about strict aliasing, when casting pointers about, but char * is defined to be safe, I think.

[OK, there are some cases, on some architectures, where pointers have special handling beyond "just being integers", but you can probably ignore those. There are also some ABIs in which different pointer types are different (FDPIC, for instance), but again, somebody else's problem.]

0
votes

Your solution is sound. Many times I've done something like this:

void handle_raw(void* data) {
    // Use a uint8_t so pointer arithmetic is at a byte-level.
    uint8_t* p = data;    
    uint16_t value;

    // Read a 2-byte value at an arbitrary offset.
    value = *(uint16_t*)(p + 0x42);

    // Write a 2-byte value at an arbitrary offset.
    *(uint32_t*)(p + 0x12) = 0xDEADBEEF;
}

Of course you should use constants for the offsets, and perform proper bounds checking.

0
votes

Is there any particular reason why you need to pack the buffers yourself? Do you really actually compress the data in the contiguous buffer before the MPI operation? If so, your compression plus transfer times must be lower than simply doing the transfer. If that is the case, then I can see that as good justification.

If you are actually packing in a contiguous buffer solely for the MPI operation, then the better solution to your problem is to stop packing the data yourself and instead use MPI derived datatypes.

You can do a web search with something like "MPI derived datatypes example", and get much better explanations than what I can briefly write here.