Hi everyone,
I have been working on an image processing library that I want to use on android. I know that performance is a bit of a problem on image processing, so I started to study some bit hacking to improve the performance on my integer operations. My problem now is that I don't know why my c-lambdas take so much time to execute. All operations take constant time and there is no branching. Probably is some sort of conversion time between C types and GAMBIT types. I expected min-integer and abs-integer to be a lot faster than min and abs, respectively. Any help is appreciated.
URL REFERENCE: http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
MY ENVIRONMENT: Linux casa 3.6.4-1-ARCH #1 SMP PREEMPT Mon Oct 29 15:13:56 CET 2012 i686 GNU/Linux
gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/lto-wrapper Target: i686-pc-linux-gnu Configured with: /build/src/gcc-4.7.2/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/--enable-languages=c,c++,ada,fortran,go,lto,objc,... --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release Thread model: posix gcc version 4.7.2 (GCC)
Gambit v4.6.6 (SINGLE HOST and GCC extensions enabled)
SCHEME CODE: compiled with ---> gambitc -exe bithacks.scm
;; bithacks.scm
(declare (not safe) (fixnum) (inline) (inline-primitives))
;; INTEGER ABSOLUTE (define abs-integer (c-lambda (int) int "// we want to find the absolute value of arg1 int const mask = ___arg1 >> sizeof(int) * CHAR_BIT - 1; ___result = (___arg1 + mask) ^ mask;"))
(define min-integer (c-lambda (int int) int "// find the smaller of arg1 arg2 ___result = ___arg2 ^ ((___arg1 ^ ___arg2) & -(___arg1 < ___arg2));"))
(define (test-abs) (let ((result 0)) (begin (display "abs:\n") (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i)) (loop-i (fx+ 1 i)))))) (display "abs-integer:\n") (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer j)) (loop-j (fx+ 1 j)))))))))
(define (test-min) (let ((result 0)) (begin (display "min:\n") (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i result)) (loop-i (fx+ 1 i)))))) (display "min-integer:\n") (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j result)) (loop-j (fx+ 1 j)))))))))
;; run tests
(test-abs) (test-min)
;;;;;;;;;;;;;;;;;;;;;; RESULTS;;;;;;;;;;;;;;;;;;;;;
abs: (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i)) (loop-i (fx+ 1 i)))))) 2 ms real time 3 ms cpu time (3 user, 0 system) no collections no bytes allocated no minor faults no major faults abs-integer: (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer j)) (loop-j (fx+ 1 j)))))) 22 ms real time 23 ms cpu time (23 user, 0 system) no collections no bytes allocated 2 minor faults no major faults min: (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i result)) (loop-i (fx+ 1 i)))))) 2 ms real time 3 ms cpu time (0 user, 3 system) no collections no bytes allocated no minor faults no major faults min-integer: (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j result)) (loop-j (fx+ 1 j)))))) 28 ms real time 30 ms cpu time (27 user, 3 system) no collections no bytes allocated no minor faults no major faults
Afficher les réponses par date
First, please add (declare (block)) and re-compile, this way Gambit won't need to re-look up the procedures in the global namespace that you call, on each invocation. Please report what kind of improvement you got with this.
Now just to make this round of emails complete:
What would be good/very good performance in your view?
Could you send jobs in some kind of batch format to C?
Note that Gambit-internal fxnum variables have a special encoding (the lowermost two(?) bits are object type identifier, which are shifted out when converting them to C int), this takes a little bit of time.
If for instance you make u32vectors/s32vectors/etc. [and send batch ops made on those to C to be performed etc.] then you can just read out the content of those vectors 'raw' in C, there's no interconversion there.
Brgds
2012/11/4 tcl super dude tcleval@gmail.com
Hi everyone,
I have been working on an image processing library that I want to use on android. I know that performance is a bit of a problem on image processing, so I started to study some bit hacking to improve the performance on my integer operations. My problem now is that I don't know why my c-lambdas take so much time to execute. All operations take constant time and there is no branching. Probably is some sort of conversion time between C types and GAMBIT types. I expected min-integer and abs-integer to be a lot faster than min and abs, respectively. Any help is appreciated.
URL REFERENCE: http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
MY ENVIRONMENT: Linux casa 3.6.4-1-ARCH #1 SMP PREEMPT Mon Oct 29 15:13:56 CET 2012 i686 GNU/Linux
gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/lto-wrapper Target: i686-pc-linux-gnu Configured with: /build/src/gcc-4.7.2/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/--enable-languages=c,c++,ada,fortran,go,lto,objc,... --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release Thread model: posix gcc version 4.7.2 (GCC)
Gambit v4.6.6 (SINGLE HOST and GCC extensions enabled)
SCHEME CODE: compiled with ---> gambitc -exe bithacks.scm
;; bithacks.scm
(declare (not safe) (fixnum) (inline) (inline-primitives))
;; INTEGER ABSOLUTE (define abs-integer (c-lambda (int) int "// we want to find the absolute value of arg1 int const mask = ___arg1 >> sizeof(int) * CHAR_BIT - 1; ___result = (___arg1 + mask) ^ mask;"))
(define min-integer (c-lambda (int int) int "// find the smaller of arg1 arg2 ___result = ___arg2 ^ ((___arg1 ^ ___arg2) & -(___arg1 < ___arg2));"))
(define (test-abs) (let ((result 0)) (begin (display "abs:\n") (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i)) (loop-i (fx+ 1 i)))))) (display "abs-integer:\n") (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer j)) (loop-j (fx+ 1 j)))))))))
(define (test-min) (let ((result 0)) (begin (display "min:\n") (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i result)) (loop-i (fx+ 1 i)))))) (display "min-integer:\n") (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j result)) (loop-j (fx+ 1 j)))))))))
;; run tests
(test-abs) (test-min)
;;;;;;;;;;;;;;;;;;;;;; RESULTS;;;;;;;;;;;;;;;;;;;;;
abs: (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i)) (loop-i (fx+ 1 i)))))) 2 ms real time 3 ms cpu time (3 user, 0 system) no collections no bytes allocated no minor faults no major faults abs-integer: (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer j)) (loop-j (fx+ 1 j)))))) 22 ms real time 23 ms cpu time (23 user, 0 system) no collections no bytes allocated 2 minor faults no major faults min: (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i result)) (loop-i (fx+ 1 i)))))) 2 ms real time 3 ms cpu time (0 user, 3 system) no collections no bytes allocated no minor faults no major faults min-integer: (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j result)) (loop-j (fx+ 1 j)))))) 28 ms real time 30 ms cpu time (27 user, 3 system) no collections no bytes allocated no minor faults no major faults
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
I assumed I could get a bit of performance improvement over min procedure because I don't do any branch. I' thought min-integer to be at least close to gambit-c time. The problem is that those operations and a few more are performed thousands of times. Gambit loops are usually 2x slower than C, so I hoped I could improve some basic operations as I only perform them on integers. I don't Understand how a simple shift to extract the lower bits could make so much difference. Maybe if someone point me to the implementation of min and abs on gambit, I can understand the difference. I ve looked for these procedures on gambit's source but could not point it out.
I'll try to implement them using vectors and see what happens.
now compiled with (block):
abs:
(time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i)) (loop-i (fx+ 1 i)))))) 2 ms real time
3 ms cpu time (3 user, 0 system)
no collections
no bytes allocated
no minor faults
no major faults
abs-integer:
(time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer j)) (loop-j (fx+ 1 j)))))) 19 ms real time
20 ms cpu time (17 user, 3 system)
no collections
no bytes allocated
2 minor faults
no major faults
min:
(time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i result)) (loop-i (fx+ 1 i)))))) 2 ms real time
3 ms cpu time (0 user, 3 system)
no collections
no bytes allocated
no minor faults no major faults min-integer: (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j result)) (loop-j (fx+ 1 j)))))) 27 ms real time 27 ms cpu time (27 user, 0 system) no collections no bytes allocated no minor faults no major faults
On Sun, Nov 4, 2012 at 5:15 PM, Mikael mikael.rcv@gmail.com wrote:
First, please add (declare (block)) and re-compile, this way Gambit won't need to re-look up the procedures in the global namespace that you call, on each invocation. Please report what kind of improvement you got with this.
Now just to make this round of emails complete:
What would be good/very good performance in your view?
Could you send jobs in some kind of batch format to C?
Note that Gambit-internal fxnum variables have a special encoding (the lowermost two(?) bits are object type identifier, which are shifted out when converting them to C int), this takes a little bit of time.
If for instance you make u32vectors/s32vectors/etc. [and send batch ops made on those to C to be performed etc.] then you can just read out the content of those vectors 'raw' in C, there's no interconversion there.
Brgds
2012/11/4 tcl super dude tcleval@gmail.com
Hi everyone,
I have been working on an image processing library that I want to use on android. I know that performance is a bit of a problem on image processing, so I started to study some bit hacking to improve the performance on my integer operations. My problem now is that I don't know why my c-lambdas take so much time to execute. All operations take constant time and there is no branching. Probably is some sort of conversion time between C types and GAMBIT types. I expected min-integer and abs-integer to be a lot faster than min and abs, respectively. Any help is appreciated.
URL REFERENCE: http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
MY ENVIRONMENT: Linux casa 3.6.4-1-ARCH #1 SMP PREEMPT Mon Oct 29 15:13:56 CET 2012 i686 GNU/Linux
gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.7.2/lto-wrapper Target: i686-pc-linux-gnu Configured with: /build/src/gcc-4.7.2/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/--enable-languages=c,c++,ada,fortran,go,lto,objc,... --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --enable-libstdcxx-time --enable-gnu-unique-object --enable-linker-build-id --with-ppl --enable-cloog-backend=isl --disable-ppl-version-check --disable-cloog-version-check --enable-lto --enable-gold --enable-ld=default --enable-plugin --with-plugin-ld=ld.gold --with-linker-hash-style=gnu --disable-multilib --disable-libssp --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-checking=release Thread model: posix gcc version 4.7.2 (GCC)
Gambit v4.6.6 (SINGLE HOST and GCC extensions enabled)
SCHEME CODE: compiled with ---> gambitc -exe bithacks.scm
;; bithacks.scm
(declare (not safe) (fixnum) (inline) (inline-primitives))
;; INTEGER ABSOLUTE (define abs-integer (c-lambda (int) int "// we want to find the absolute value of arg1 int const mask = ___arg1 >> sizeof(int) * CHAR_BIT - 1; ___result = (___arg1 + mask) ^ mask;"))
(define min-integer (c-lambda (int int) int "// find the smaller of arg1 arg2 ___result = ___arg2 ^ ((___arg1 ^ ___arg2) & -(___arg1 < ___arg2));"))
(define (test-abs) (let ((result 0)) (begin (display "abs:\n") (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i)) (loop-i (fx+ 1 i)))))) (display "abs-integer:\n") (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer j)) (loop-j (fx+ 1 j)))))))))
(define (test-min) (let ((result 0)) (begin (display "min:\n") (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i result)) (loop-i (fx+ 1 i)))))) (display "min-integer:\n") (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j result)) (loop-j (fx+ 1 j)))))))))
;; run tests
(test-abs) (test-min)
;;;;;;;;;;;;;;;;;;;;;; RESULTS;;;;;;;;;;;;;;;;;;;;;
abs: (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (abs i)) (loop-i (fx+ 1 i)))))) 2 ms real time 3 ms cpu time (3 user, 0 system) no collections no bytes allocated no minor faults no major faults abs-integer: (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (abs-integer j)) (loop-j (fx+ 1 j)))))) 22 ms real time 23 ms cpu time (23 user, 0 system) no collections no bytes allocated 2 minor faults no major faults min: (time (let loop-i ((i 0)) (cond ((fx< i 100000) (set! result (min i result)) (loop-i (fx+ 1 i)))))) 2 ms real time 3 ms cpu time (0 user, 3 system) no collections no bytes allocated no minor faults no major faults min-integer: (time (let loop-j ((j 0)) (cond ((fx< j 100000) (set! result (min-integer j result)) (loop-j (fx+ 1 j)))))) 28 ms real time 30 ms cpu time (27 user, 3 system) no collections no bytes allocated no minor faults no major faults
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
http://c2.com/cgi/wiki?PrematureOptimization
I rewrote your code to magnify the difference between inlined abs and min and FFI abs and min. I came up with this:
;; ffi-test.scm
(declare (standard-bindings) (extended-bindings) (block) (fixnum) (not safe) ;; the following is not generally recommended (not interrupts-enabled))
;; INTEGER ABSOLUTE (define abs-integer (c-lambda (int) int "// we want to find the absolute value of arg1 int const mask = ___arg1 >> sizeof(int) * CHAR_BIT - 1; ___result = (___arg1 + mask) ^ mask;"))
(define min-integer (c-lambda (int int) int "// find the smaller of arg1 arg2 ___result = ___arg2 ^ ((___arg1 ^ ___arg2) & -(___arg1 < ___arg2));"))
(define N 10000000)
(define (test-abs) (let ((result (time (let loop-i ((i 0) (result 0)) (if (fx< i N) (loop-i (fx+ i 1) (abs i)) result))))) (time (let loop-j ((j 0) (result result)) (if (fx< j N) (loop-j (fx+ j 1) (abs-integer j)) result)))))
(define (test-min) (let ((result (time (let loop-i ((i 0) (result 0)) (if (fx< i N) (loop-i (fx+ i 1) (min result i)) result))))) (time (let loop-j ((j 0) (result result)) (if (fx< j N) (loop-j (fx+ j 1) (min-integer result j)) result)))))
;; run tests
(test-abs) (test-min)
The result is
heine:~/Downloads> gsc -keep-c -cc-options '-save-temps' ffi-test heine:~/Downloads> gsi ffi-test (time (let loop-i ((i 0) (result 0)) (if (fx< i N) (loop-i (fx+ i 1) (abs i)) result))) 9 ms real time 8 ms cpu time (8 user, 0 system) no collections no bytes allocated no minor faults no major faults (time (let loop-j ((j 0) (result result)) (if (fx< j N) (loop-j (fx+ j 1) (abs-integer j)) result))) 357 ms real time 356 ms cpu time (356 user, 0 system) no collections no bytes allocated no minor faults no major faults (time (let loop-i ((i 0) (result 0)) (if (fx< i N) (loop-i (fx+ i 1) (min result i)) result))) 15 ms real time 16 ms cpu time (16 user, 0 system) no collections no bytes allocated no minor faults no major faults (time (let loop-j ((j 0) (result result)) (if (fx< j N) (loop-j (fx+ j 1) (min-integer result j)) result))) 459 ms real time 460 ms cpu time (460 user, 0 system) no collections no bytes allocated no minor faults no major faults
Search for ^ in ffi-test.i and ffi-test.c to see how your C code is included in the generated C code.
So starting by worrying about how low-level features are implemented in Gambit is totally the wrong way around. Start by learning how to use the options and declarations that Gambit offers. I use Gambit all the time for image processing, and I don't have any problems with speed.
Brad