Jump to content

  • Log In with Google      Sign In   
  • Create Account


Understanding XMVECTOR parameter passing to own functions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Chris_J_H   Members   -  Reputation: 256

Like
0Likes
Like

Posted 11 January 2013 - 03:35 AM

Hi - I am having a bit of difficulty understanding exactly what the documentation is telling me on this... I am using Visual C++ 2010 express compiling for 32 or 64 bit implementation (Windows 7). I am using the XNA Math library (xnamath.h) and wish to write some of my own functions to handle more complex collision detection where I pass XMVECTOR parameters (going further than xnacollision.h). As I understand it, to achieve reasonable optimization, I should:

1) inline the functions

      ** This is because standard function call conventions are not great at allowing register passing of SIMD registers, so call overhead best avoided if poss...?**

2) specify _fastcall in function declaration.

3) pass FXMVECTOR for 1st 3 args and CXMVECTOR thereafter.

      ** 2 & 3 give the best chance of an optimal transfer of values within the SIMD registers if the inline is ignored by the compiler? **

      ** For x64 the docs seem to imply no possibility of SIMD register passing in a function call - so I have to hope it inlines... is that right? **

Thanks for any comments/explanations/insight.



Sponsor:

#2 mrheisenberg   Members   -  Reputation: 356

Like
1Likes
Like

Posted 13 January 2013 - 06:44 PM

I actualy tried to benchmark some DirectXMath code,one part was doing "unoptimized" functions,by taking XMFLOAT4X4 and XMFLOAT4 as arguments,then loading them into XMVECTOR/XMMATRIX,then doing operations,then storing again,passing again to another function that loads again and then stores again and outputs to the progra.The other test functions were doing the same operations,but with only loading/unloading when it has to output,otherwise taking CXMMATRIX and FXMVECTOR as arguments and being as fast as possible.Even with millions of calls,the "optimized" version was only slightly faster than the "unoptimized" one.



#3 mrheisenberg   Members   -  Reputation: 356

Like
1Likes
Like

Posted 13 January 2013 - 06:50 PM

Maybe I'm doing it wrong?

Profiler.hpp

#pragma once

#ifndef _PROFILER_HPP_
#define _PROFILER_HPP_

#pragma comment (lib,"Winmm.lib")

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>

class Profiler
{
public:
	static Profiler* Instance();
	static void Destroy();

	void operator()();
	UINT64 GetDTime()
	{
		return(DTime);
	}

private:
	Profiler();
	~Profiler();

	static Profiler *instance;
	UINT64 lastGameTime;
	UINT64 DTime;
};

#endif 

Benchmarks.hpp

#pragma once

#ifndef _TEST_FUNCTIONS_HPP_
#define _TEST_FUNCTIONS_HPP_

#include <Windows.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <fstream>
#include <ostream>
#include <istream>
#include <DirectXMath.h>
using namespace DirectX;

namespace Benchmark
{
	XMFLOAT4X4 UnoptA(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec);
	XMFLOAT4X4 UnoptB(XMFLOAT4X4 inputMat);

	XMMATRIX OptA(CXMMATRIX inputMat, FXMVECTOR inputVec);
	XMMATRIX OptB(CXMMATRIX inputMat);

	void BenchUnopt(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec, UINT count, std::vector<XMFLOAT4X4> &buffer);
	void BenchOpt(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec, UINT count, std::vector<XMFLOAT4X4> &buffer);
}

#endif

Profiler.cpp

#include "Profiler.hpp"

Profiler *Profiler::instance = nullptr;

Profiler *Profiler::Instance()
{
	if(instance)
	{
		return(instance);
	}
	return(instance = new Profiler);
}

void Profiler::Destroy()
{
	if(instance)
	{
		delete instance;
	}
}

Profiler::Profiler(): DTime(0)
{
	lastGameTime = (UINT64)timeGetTime();
}

Profiler::~Profiler()
{

}

void Profiler::operator()()
{
	UINT64 ticks = (UINT64)timeGetTime();

	DTime = ticks - lastGameTime;

	lastGameTime = ticks;
}

BenchmarksOpt.cpp

#include "Benchmarks.hpp"

namespace Benchmark
{
	XMMATRIX OptA(CXMMATRIX inputMat, FXMVECTOR inputVec)
	{
		return(OptB(XMMatrixMultiply(inputMat, XMMatrixRotationQuaternion(inputVec))));
	}

	XMMATRIX OptB(CXMMATRIX inputMat)
	{
		XMVECTOR determinant;
		return(XMMatrixInverse(&determinant, inputMat));
	}

	void BenchOpt(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec, UINT count, std::vector<XMFLOAT4X4> &buffer)
	{
		XMMATRIX matrixInput = XMLoadFloat4x4(&inputMat);
		XMVECTOR vectorInput = XMLoadFloat4(&inputVec);

		for(UINT i = 0; i < count; i++)
		{
			XMFLOAT4X4 bufferMat;
			XMStoreFloat4x4(&bufferMat, OptA(matrixInput, vectorInput));
			buffer.push_back(bufferMat);
		}
	}
}

BenchmarksUnopt.cpp

#include "Benchmarks.hpp"

namespace Benchmark
{
	XMFLOAT4X4 UnoptA(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec)
	{
		XMMATRIX matrixInput = XMLoadFloat4x4(&inputMat);
		XMVECTOR vectorInput = XMLoadFloat4(&inputVec);
		
		matrixInput = XMMatrixMultiply(matrixInput, XMMatrixRotationQuaternion(vectorInput));

		XMFLOAT4X4 outputMat;
		XMStoreFloat4x4(&outputMat, matrixInput);

		return(UnoptB(outputMat));
	}

	XMFLOAT4X4 UnoptB(XMFLOAT4X4 inputMat)
	{
		XMMATRIX matrixInput = XMLoadFloat4x4(&inputMat);
		XMVECTOR determinant;
		matrixInput = XMMatrixInverse(&determinant, matrixInput);
		XMFLOAT4X4 outputMat;
		XMStoreFloat4x4(&outputMat, matrixInput);
		
		return(outputMat);
	}

	void BenchUnopt(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec, UINT count, std::vector<XMFLOAT4X4> &buffer)
	{
		for(UINT i = 0; i < count; i++)
		{
			buffer.push_back(UnoptA(inputMat, inputVec));
		}
	}
}

main.cpp

#include "Benchmarks.hpp"
#include "Profiler.hpp"

using namespace std;
using namespace Benchmark;

int main()
{
	Profiler &ProfilerRef = *Profiler::Instance();
	UINT count;
	cout<<"Select iteration amount:";
	cin>>count;
	cout<<endl<<"Test will now begin"<<endl<<endl;

	std::vector<XMFLOAT4X4> bufferUnopt;
	std::vector<XMFLOAT4X4> bufferOpt;
	bufferUnopt.reserve(count);
	bufferOpt.reserve(count);

	XMFLOAT4 testVector = XMFLOAT4(1.0f, 3.0f, 2.0f, 4.0f);
	XMFLOAT4X4 testMatrix = XMFLOAT4X4(3.0f,3.0f,3.0f,3.0f,
									   3.0f,3.0f,3.0f,3.0f,
									   3.0f,3.0f,3.0f,3.0f,
									   3.0f,3.0f,3.0f,3.0f);

	ProfilerRef();
	BenchUnopt(testMatrix, testVector, count, bufferUnopt);
	ProfilerRef();
	cout<<"Unoptimized Benchmkar: "<<ProfilerRef.GetDTime()<<endl;

	ProfilerRef();
	BenchOpt(testMatrix, testVector, count, bufferOpt);
	ProfilerRef();
	cout<<"Optimized Benchmkar: "<<ProfilerRef.GetDTime()<<endl;

	ofstream myfile;
	myfile.open ("example.txt");
	for(UINT i = 0; i < bufferOpt.size(); i++)
	{
		myfile<<bufferOpt[i]._11;
	}
	for(UINT i = 0; i < bufferUnopt.size(); i++)
	{
		myfile<<bufferUnopt[i]._11;
	}
	myfile.close();

	return 0;
}

I split the Benchmarks# into 2 .cpp files,so I can get 2 seperate ASM/Machine code lists,the Unopt version always gets over 20 times more output,but the speed difference is almosst 0



#4 Chris_J_H   Members   -  Reputation: 256

Like
0Likes
Like

Posted 14 January 2013 - 04:32 AM

MrHeisenberg - thanks for your observations. I ran your code on my pc (replacing your test matrix with the identity matrix as it was giving error values in my output, 5mm iterations). I see a 12% speed gain for the optimized functions on x64 and 27% gain on x86 compilations. Given I really only care about x64 at this point, I agree, it doesn't seem a big deal... Then I tested: 1) add inline to the optimized functions and 2) declaring them as _fastcall (which the docs recommend) - neither seem to make any difference within the noise of the data for either x86 or x64. I Then refactored the code to eliminate the calls completely for the optimized calculation: to get a total 21% gain in x64 and 33% total gain in x86 which starts to be more significant. I checked my compiler (VC++) setting for inlining functions: it is at the max (/Ob2): if I change the inline declaration to _forceinline (!) I finally get the same effect using functions as if I had refactored the code... hmm maybe some other settings on the compiler are interfering...






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS