• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ongamex92

How to profile SIMD

13 posts in this topic

Actually the topic name should be "How to profile code". 

 

Well I am writing SIMD math library. I got 2 implementations SSE and scalar.

I'm not shure how measure the code speed. Currently Im not using optimization, and no debug symbols are generated for profiling.

I'm creating a loop that repeats the operation...

 

The compiler is cl

 

I'm expecting SSE dot product to be slower than scalar version?

But the cross product is also slower!?!@

 

SGE_FORCE_INLINE SGVector vec3_cross(const SGVector& a, const SGVector& b)
{
#if defined(SGE_MATH_USE_SSE)
__m128 T = _mm_shuffle_ps(a.m_M128, a.m_M128, SGE_SIMD_SHUFFLE(1, 2, 0, 3)); //(Y Z X 0)
__m128 V = _mm_shuffle_ps(b.m_M128, b.m_M128, SGE_SIMD_SHUFFLE(1, 2, 0, 3)); //(Y Z X 0)


//i(ay*bz - by*az)  + j(bx*az - ax*bz)  + k(ax*by - bx*ay)
T = _mm_mul_ps(T, b.m_M128);//bx * ay, by * az, bz * ax
V = _mm_mul_ps(V, a.m_M128);//ax * by, ay * bz, az * bx
V = _mm_sub_ps(V, T);


V = _mm_shuffle_ps(V, V, SGE_SIMD_SHUFFLE(1, 2, 0, 3));
return SGVector(V);
#else
const float x = (a.y*b.z) - (b.y*a.z);
const float y = (b.x*a.z) - (a.x*b.z);
const float z = (a.x*b.y) - (b.x,a.y);


return SGVector(x, y, z, 0.f);
#endif
}

where SGVector is struct with union{ struct {float x,y,z;}; float arr[4]; __m128 m_M128}. (maybe that is the problem?!)

 

EDIT : maybe __forceinline is involed too!? I will remove it.

Edited by imoogiBG
0

Share this post


Link to post
Share on other sites


Currently Im not using optimization

 

Ah? If you want to measure performance you need to compile using optimizations. at least /O2, otherwise every instruction will go through memory.

 

The best tool for profiling is Intel's VTune, they have 30 days free trial. Visual studio comes with a profiler you can use as well. If you want to profile yourself, you can use the timestamp counter (rdtcs instruction, visual studio has an intinsic).

 

__forceinline is very good for performance, I use it almost everywhere in performance critical code.

 

The problem is probably the shuffles. Not sure what CPU you are using, but shuffle performance is limited. You have 3 shuffles and 3 math instructions, which is not a good ratio. The SSE math instructions have data dependency on the shuffles, which will stall the CPU pipeline.

Also, your inputs and outputs are structs. Even though you pass them by reference, both shuffles and return values will got through memory (probably, need to look at the generated assembly).

 

To maximize SSE performance you need to design your whole code around it. Have long SSE math sequences, and reduce the amount of memory acceses and shuffles.

 

BTW, you can tell CL to use scalar SSE for floating-point instructions by using /arch:SSE. Check this.

Edited by satanir
0

Share this post


Link to post
Share on other sites

 

Pass vectors by value, not reference

 

 

Hi Rob,

 

Why do you recommend passing vectors by value rather than reference?

I've made it a habit to use const ref parameters wherever possible, and wherever ref makes sense
compared to the size of the type (I wouldn't pass a char by reference)

0

Share this post


Link to post
Share on other sites


If you'd ever care to go into depth about optimization work, consider writing an article for the site wink.png

Maybe I willsmile.png

0

Share this post


Link to post
Share on other sites

Guys i was looking at the ASM and this i what i've got

 

//pure _128
__m128 a, b , c;
c = _mm_set_ps1(f);
 movss       xmm1,dword ptr [esp+0Ch]  
b = _mm_set_ps1(ff);
 movss       xmm0,dword ptr [esp+10h]  
 shufps      xmm0,xmm0,0  
 shufps      xmm1,xmm1,0  
a = _mm_add_ps(c, b);
 addps       xmm1,xmm0  

/////////////////////////////////////////////////////
//__m128 as a member of a struct
SGVector v, v2, d;
d.m_M128 = _mm_set_ps1(f);
 movss       xmm1,dword ptr [esp+0Ch]  
v2.m_M128 = _mm_set_ps1(ff);
 movss       xmm0,dword ptr [esp+10h]  
 shufps      xmm0,xmm0,0  
 shufps      xmm1,xmm1,0  
v.m_M128 = _mm_add_ps(d.m_M128, v2.m_M128);
 addps       xmm1,xmm0  

?/////////////////////////////////////////////////////
//_m128 as a member of a struct. Calling a custom function
  SGVector v, v2, d;
d.m_M128 = _mm_set_ps1(f);
 movss       xmm1,dword ptr [esp+0Ch]  
v2.m_M128 = _mm_set_ps1(ff);
 movss       xmm0,dword ptr [esp+10h]  
 shufps      xmm0,xmm0,0  
 shufps      xmm1,xmm1,0  
vec3_add2(d, v2, v);
 addps       xmm1,xmm0 

////////////////////////////////////////////
//retval is SGVector 
SGVector v, v2, d;
d.m_M128 = _mm_set_ps1(f);
 movss       xmm1,dword ptr [esp+0Ch]  
v2.m_M128 = _mm_set_ps1(ff);
 movss       xmm0,dword ptr [esp+10h]  
 shufps      xmm0,xmm0,0  
 shufps      xmm1,xmm1,0  
v = vec3_add(d.m_M128, v2.m_M128);
 addps       xmm1,xmm0   
SGE_FORCE_INLINE void vec3_add2(const SGVector& a, const SGVector& b, SGVector& c)
{
#if defined(SGE_MATH_USE_SSE)
c.m_M128 = _mm_add_ps(a.m_M128, b.m_M128);
#endif
}
Refs, retvals do not change anything. For add ofc. Tomorrow i will try the cross product.
 
CL x86 O2
 
PS: 
 shufps      xmm0,xmm0,0  

 shufps      xmm1,xmm1,0  

 

Why shufps is needed?

 

PS 2:

 

SuperVGA, on 24 Oct 2013 - 2:11 PM, said:

 

 

Pass vectors by value, not reference

 

 

Hi Rob,

 

Why do you recommend passing vectors by value rather than reference?

I've made it a habit to use const ref parameters wherever possible, and wherever ref makes sense
compared to the size of the type (I wouldn't pass a char by reference)

 

 

if the function is inlined then the refs do not change anything.

Edited by imoogiBG
0

Share this post


Link to post
Share on other sites

Pass vectors by value, not reference

 
Hi Rob,
 
Why do you recommend passing vectors by value rather than reference?
I've made it a habit to use const ref parameters wherever possible, and wherever ref makes sense
compared to the size of the type (I wouldn't pass a char by reference)

As of C++11, the new rule of thumb is to pass vectors and other objects like it by value when the operation performed on them requires a copy, otherwise pass by const ref. The rule for passing by reference is still the same. The reason is that this can enable move semantics when the arguments passed are rvalues. With virtual functions, the rule becomes when the operation logically needs a copy (even though every overload may not).

EDIT: that's std::vector. SIMD vectors that your architecture natively supports are basically primitive types since they can fit into a single register by definition. Edited by King Mir
2

Share this post


Link to post
Share on other sites


shufps xmm0,xmm0,0

shufps xmm1,xmm1,0

Why shufps is needed?

That's what _mm_set_ps1() - broadcast a single float value into all of the __m128 components. Has to be movss and shufps.


Refs, retvals do not change anything

This because of the inlining. You usually don't need to use reference for __m128. I use references for __m128 only when passing more than 3 vectors to a function - no calling convention support that, and due to stack alignemnt you can't pass a vector on stack.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0