Optimization problem

Discussion in 'C++' started by hamze, May 22, 2011.

  1. hamze

    hamze Guest

    I have a special device in my system, I must work with it in this
    manner that : write 4 bytes and read 3 bytes,
    this device has no reset, so if one of reads or writes miss, from that
    time device will not work correctly because order of writes and reads
    are missed.

    this device works right when optimization is off, but when I turn it
    on it stop working properly in my code.
    some time i did not use one of its read value, so I think optimization
    remove it and order of read and writes are missed.
    how can I solve this problem ?
    I also test this solution that I defined an asm valotile ( ) and put
    all reads and write inside it but still turning optimization on
    corrupt my program result.
     
    hamze, May 22, 2011
    #1
    1. Advertising

  2. On May 21, 11:29 pm, hamze <> wrote:
    > I have a special device in my system, I must work with it in this
    > manner that : write 4 bytes and read 3 bytes,
    > this device has no reset, so if one of reads or writes miss, from that
    > time device will not work correctly because order of writes and reads
    > are missed.
    >
    > this device works right when optimization is off, but when I turn it
    > on it stop working properly in my code.
    > some time i did not use one of its read value, so I think optimization
    > remove it and order of read and writes are missed.
    > how can I solve this problem ?
    > I also test this solution that I defined an asm valotile ( ) and put
    > all reads and write inside it but still turning optimization on
    > corrupt my program result.


    From what I've heard in this group, volatile alone is insufficient for
    correct MMIO (memory mapped IO) on most systems. I would suggest
    looking at the docs for your implementation to see what it says about
    correct MMIO. You could also look at the output assembly to see if it
    is indeed not doing as you want.
     
    Joshua Maurice, May 22, 2011
    #2
    1. Advertising

  3. hamze

    hamze Guest

    On May 22, 12:25 pm, Joshua Maurice <> wrote:
    > On May 21, 11:29 pm, hamze <> wrote:
    >
    > > I have a special device in my system, I must work with it in this
    > > manner that : write 4 bytes and read 3 bytes,
    > > this device has no reset, so if one of reads or writes miss, from that
    > > time device will not work correctly because order of writes and reads
    > > are missed.

    >
    > > this device works right when optimization is off, but when I turn it
    > > on it stop working properly in my code.
    > > some time i did not use one of its read value, so I think optimization
    > > remove it and order of read and writes are missed.
    > > how can I solve this problem ?
    > > I also test this solution that I defined an asm valotile ( ) and put
    > > all reads and write inside it but still turning optimization on
    > > corrupt my program result.

    >
    > From what I've heard in this group, volatile alone is insufficient for
    > correct MMIO (memory mapped IO) on most systems. I would suggest
    > looking at the docs for your implementation to see what it says about
    > correct MMIO. You could also look at the output assembly to see if it
    > is indeed not doing as you want.


    thanks alot, you gave me a valuable clue,
    I know what is my hardware correct order of MMIO, but I dont know how
    to make compiler know about it and force compile not to REORDER my
    code.
     
    hamze, May 22, 2011
    #3
  4. hamze

    Jorgen Grahn Guest

    On Sun, 2011-05-22, hamze wrote:
    > On May 22, 12:25 pm, Joshua Maurice <> wrote:
    >> On May 21, 11:29 pm, hamze <> wrote:
    >>
    >> > I have a special device in my system, I must work with it in this
    >> > manner that : write 4 bytes and read 3 bytes,
    >> > this device has no reset, so if one of reads or writes miss, from that
    >> > time device will not work correctly because order of writes and reads
    >> > are missed.

    >>
    >> > this device works right when optimization is off, but when I turn it
    >> > on it stop working properly in my code.
    >> > some time i did not use one of its read value, so I think optimization
    >> > remove it and order of read and writes are missed.
    >> > how can I solve this problem ?
    >> > I also test this solution that I defined an asm valotile ( ) and put
    >> > all reads and write inside it but still turning optimization on
    >> > corrupt my program result.

    >>
    >> From what I've heard in this group, volatile alone is insufficient for
    >> correct MMIO (memory mapped IO) on most systems. I would suggest
    >> looking at the docs for your implementation to see what it says about
    >> correct MMIO. You could also look at the output assembly to see if it
    >> is indeed not doing as you want.

    >
    > thanks alot, you gave me a valuable clue,
    > I know what is my hardware correct order of MMIO, but I dont know how
    > to make compiler know about it and force compile not to REORDER my
    > code.


    It's possible that the right thing to do is to write the critical
    parts in assembly. You can start with compiler-generated assembly and
    modify that, to make it a bit easier.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 22, 2011
    #4
  5. hamze

    Nobody Guest

    On Sun, 22 May 2011 01:47:20 -0700, hamze wrote:

    > I know what is my hardware correct order of MMIO, but I dont know how
    > to make compiler know about it and force compile not to REORDER my
    > code.


    The compiler will not re-order reads/writes of volatile variables with
    respect to each other. However, the CPU might perform re-ordering; to
    prevent this, you need to use a memory barrier (aka "fence").

    http://en.wikipedia.org/wiki/Memory_barrier
    http://en.wikipedia.org/wiki/Memory_ordering

    For gcc, see e.g.:

    http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Atomic-Builtins.html

    Also, you might try comp.arch.embedded.
     
    Nobody, May 22, 2011
    #5
  6. hamze

    Geoff Guest

    On Sun, 22 May 2011 01:47:20 -0700 (PDT), hamze <>
    wrote:

    >On May 22, 12:25 pm, Joshua Maurice <> wrote:
    >> On May 21, 11:29 pm, hamze <> wrote:
    >>
    >> > I have a special device in my system, I must work with it in this
    >> > manner that : write 4 bytes and read 3 bytes,
    >> > this device has no reset, so if one of reads or writes miss, from that
    >> > time device will not work correctly because order of writes and reads
    >> > are missed.

    >>
    >> > this device works right when optimization is off, but when I turn it
    >> > on it stop working properly in my code.
    >> > some time i did not use one of its read value, so I think optimization
    >> > remove it and order of read and writes are missed.
    >> > how can I solve this problem ?
    >> > I also test this solution that I defined an asm valotile ( ) and put
    >> > all reads and write inside it but still turning optimization on
    >> > corrupt my program result.

    >>
    >> From what I've heard in this group, volatile alone is insufficient for
    >> correct MMIO (memory mapped IO) on most systems. I would suggest
    >> looking at the docs for your implementation to see what it says about
    >> correct MMIO. You could also look at the output assembly to see if it
    >> is indeed not doing as you want.

    >
    >thanks alot, you gave me a valuable clue,
    >I know what is my hardware correct order of MMIO, but I dont know how
    >to make compiler know about it and force compile not to REORDER my
    >code.


    Get to know your compiler and target CPU. Microsoft provides the
    #pragma optimize directive for turning optimizations off/on for
    individual functions.

    Write your low-level MM I/O functions in assembler, call them from C.
     
    Geoff, May 22, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JE
    Replies:
    0
    Views:
    465
  2. ashu
    Replies:
    5
    Views:
    1,206
    umerarain
    May 16, 2006
  3. JE
    Replies:
    2
    Views:
    404
    Karl Heinz Buchegger
    Aug 5, 2004
  4. DENG
    Replies:
    6
    Views:
    315
  5. Ravikiran

    Zero Optimization and Sign Optimization???

    Ravikiran, Nov 17, 2008, in forum: C Programming
    Replies:
    22
    Views:
    903
    Thad Smith
    Nov 24, 2008
Loading...

Share This Page