Commit 0ab467b3 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+20 −0
Changes for AoC_codes_full/Chapter 10/Example 10-1/Depth-First Search.sln: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual C++ Express 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Depth-First Search", "Depth-First Search\Depth-First Search.vcproj", "{53D61C55-950F-4F3F-98D5-F7BD1627E3E4}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Win32 = Debug|Win32
		Release|Win32 = Release|Win32
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{53D61C55-950F-4F3F-98D5-F7BD1627E3E4}.Debug|Win32.ActiveCfg = Debug|Win32
		{53D61C55-950F-4F3F-98D5-F7BD1627E3E4}.Debug|Win32.Build.0 = Debug|Win32
		{53D61C55-950F-4F3F-98D5-F7BD1627E3E4}.Release|Win32.ActiveCfg = Release|Win32
		{53D61C55-950F-4F3F-98D5-F7BD1627E3E4}.Release|Win32.Build.0 = Release|Win32
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal
+193 −0
Changes for AoC_codes_full/Chapter 10/Example 10-1/Depth-First Search/Depth-First Search.vcproj: 193 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
	ProjectType="Visual C++"
	Version="9.00"
	Name="Depth-First Search"
	ProjectGUID="{53D61C55-950F-4F3F-98D5-F7BD1627E3E4}"
	RootNamespace="DepthFirstSearch"
	Keyword="Win32Proj"
	TargetFrameworkVersion="196613"
	>
	<Platforms>
		<Platform
			Name="Win32"
		/>
	</Platforms>
	<ToolFiles>
	</ToolFiles>
	<Configurations>
		<Configuration
			Name="Debug|Win32"
			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
			IntermediateDirectory="$(ConfigurationName)"
			ConfigurationType="1"
			CharacterSet="1"
			>
			<Tool
				Name="VCPreBuildEventTool"
			/>
			<Tool
				Name="VCCustomBuildTool"
			/>
			<Tool
				Name="VCXMLDataGeneratorTool"
			/>
			<Tool
				Name="VCWebServiceProxyGeneratorTool"
			/>
			<Tool
				Name="VCMIDLTool"
			/>
			<Tool
				Name="VCCLCompilerTool"
				Optimization="0"
				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
				MinimalRebuild="true"
				BasicRuntimeChecks="3"
				RuntimeLibrary="3"
				UsePrecompiledHeader="0"
				WarningLevel="3"
				DebugInformationFormat="4"
			/>
			<Tool
				Name="VCManagedResourceCompilerTool"
			/>
			<Tool
				Name="VCResourceCompilerTool"
			/>
			<Tool
				Name="VCPreLinkEventTool"
			/>
			<Tool
				Name="VCLinkerTool"
				LinkIncremental="2"
				GenerateDebugInformation="true"
				SubSystem="1"
				TargetMachine="1"
			/>
			<Tool
				Name="VCALinkTool"
			/>
			<Tool
				Name="VCManifestTool"
			/>
			<Tool
				Name="VCXDCMakeTool"
			/>
			<Tool
				Name="VCBscMakeTool"
			/>
			<Tool
				Name="VCFxCopTool"
			/>
			<Tool
				Name="VCAppVerifierTool"
			/>
			<Tool
				Name="VCPostBuildEventTool"
			/>
		</Configuration>
		<Configuration
			Name="Release|Win32"
			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
			IntermediateDirectory="$(ConfigurationName)"
			ConfigurationType="1"
			CharacterSet="1"
			WholeProgramOptimization="1"
			>
			<Tool
				Name="VCPreBuildEventTool"
			/>
			<Tool
				Name="VCCustomBuildTool"
			/>
			<Tool
				Name="VCXMLDataGeneratorTool"
			/>
			<Tool
				Name="VCWebServiceProxyGeneratorTool"
			/>
			<Tool
				Name="VCMIDLTool"
			/>
			<Tool
				Name="VCCLCompilerTool"
				Optimization="2"
				EnableIntrinsicFunctions="true"
				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
				RuntimeLibrary="2"
				EnableFunctionLevelLinking="true"
				UsePrecompiledHeader="0"
				WarningLevel="3"
				DebugInformationFormat="3"
			/>
			<Tool
				Name="VCManagedResourceCompilerTool"
			/>
			<Tool
				Name="VCResourceCompilerTool"
			/>
			<Tool
				Name="VCPreLinkEventTool"
			/>
			<Tool
				Name="VCLinkerTool"
				LinkIncremental="1"
				GenerateDebugInformation="true"
				SubSystem="1"
				OptimizeReferences="2"
				EnableCOMDATFolding="2"
				TargetMachine="1"
			/>
			<Tool
				Name="VCALinkTool"
			/>
			<Tool
				Name="VCManifestTool"
			/>
			<Tool
				Name="VCXDCMakeTool"
			/>
			<Tool
				Name="VCBscMakeTool"
			/>
			<Tool
				Name="VCFxCopTool"
			/>
			<Tool
				Name="VCAppVerifierTool"
			/>
			<Tool
				Name="VCPostBuildEventTool"
			/>
		</Configuration>
	</Configurations>
	<References>
	</References>
	<Files>
		<Filter
			Name="Source Files"
			Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
			UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
			>
			<File
				RelativePath=".\dfs.cpp"
				>
			</File>
		</Filter>
		<Filter
			Name="Header Files"
			Filter="h;hpp;hxx;hm;inl;inc;xsd"
			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
			>
		</Filter>
		<Filter
			Name="Resource Files"
			Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
			UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
			>
		</Filter>
	</Files>
	<Globals>
	</Globals>
</VisualStudioProject>
+71 −0
Changes for AoC_codes_full/Chapter 10/Example 10-1/Depth-First Search/dfs.cpp: 71 added lines, 0 removed lines.
Original line number Diff line number Diff line
// Serial version of Depth-first Search

#include <windows.h>
#include <stdio.h>

#define V 10

int visited[V];
int adj[V][V];
int count = 0;


void visit(int k)
{
	int i;

	visited[k] = ++count;
	 // Do something to VISIT node k
	for (i = 0; i < V; i++)
	{
		if (adj[k][i]) 
			if (!visited[i]) visit(i);
	}
}

void DFSearch()
{
	int k;

	for (k = 0; k < V; k++) visited[k] = 0;
	for (k = 0; k < V; k++) 
		if (!visited[k]) visit(k);
}

void init_data()
{
	int i,j, k;
	for (i = 0; i < V; i++) {
		for (j = 0; j < V; j++) 
			adj[i][j] = 0;
		visited[i] = 0;
	}
	for (k = 0; k < 5*V; k++){
		i = rand() % V;
		j = rand() % V;
		if (i != j) adj[i][j] = 1;
	}
}

int main(int argc, char* argv[])
{	
	init_data();
	initStack();
	for (int i=0; i<V; i++){
		printf("%3d: ",i);
		for (int j=0; j<V; j++)
			printf ("%d ",adj[i][j]);
		printf("\n");
	}

	DFSearch();

	for (int r = 0; r < V; r++) {

		printf("Node %3d visited %3d\n", r, visited[r] );

	}
	printf("\n\n");

	return 0;
}
 No newline at end of file
+20 −0
Changes for AoC_codes_full/Chapter 10/Example 10-10/MST-Prim.sln: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Prim-TBB", "Prim-TBB\Prim-TBB.vcproj", "{408531E1-ECF3-4321-B03B-CBA8062F8B46}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Win32 = Debug|Win32
		Release|Win32 = Release|Win32
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{408531E1-ECF3-4321-B03B-CBA8062F8B46}.Debug|Win32.ActiveCfg = Debug|Win32
		{408531E1-ECF3-4321-B03B-CBA8062F8B46}.Debug|Win32.Build.0 = Debug|Win32
		{408531E1-ECF3-4321-B03B-CBA8062F8B46}.Release|Win32.ActiveCfg = Release|Win32
		{408531E1-ECF3-4321-B03B-CBA8062F8B46}.Release|Win32.Build.0 = Release|Win32
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal
+137 −0
Changes for AoC_codes_full/Chapter 10/Example 10-10/Prim-TBB/Prim-TBB.cpp: 137 added lines, 0 removed lines.
Original line number Diff line number Diff line
// Parallel version of Prim's MST algorithm using OpenMP and Intel TBB

#include <windows.h>
#include <float.h>
#include <stdio.h>
#include "tbb/task_scheduler_init.h"
#include "tbb/parallel_reduce.h"
#include "tbb/blocked_range.h"


using namespace tbb;
#define V 20

class NearestNeighbor {
	const float *const NNDist;

public:
	float minDistVal;
	int minDistIndex;

	void operator()(const blocked_range<int>& r) {
		for(int j = r.begin(); j != r.end(); ++j) {
			if (0 <= NNDist[j] && NNDist[j] < minDistVal) {
				minDistVal = NNDist[j];
				minDistIndex = j;
			}
		}
	}

	void join( const NearestNeighbor& y ) {
		if (y.minDistVal < minDistVal) {
			minDistVal = y.minDistVal;
			minDistIndex = y.minDistIndex;
		}
	}

	NearestNeighbor( const float *nnd ) :
		NNDist(nnd), minDistVal(FLT_MAX), minDistIndex(-1) {}

	NearestNeighbor( NearestNeighbor& x, split ) :
		NNDist(x.NNDist), minDistVal(FLT_MAX), minDistIndex(-1) {}
};

int NodeNearestToTree(float *a, int n) 
{
	NearestNeighbor Node(a);
	parallel_reduce(blocked_range<int>(0,n), Node, auto_partitioner());
	return Node.minDistIndex;
}

void cPrims(float **W, int **T, int N)
{
  int i, j, k;
  int *nearNode = new int[N];
  float *minDist = new float[N];
  float min;

  for (i = 1; i < N; i++) {
	  nearNode[i] = 0;
	  minDist[i] = W[i][0];
  }
  for (i = 0; i < N-1; i++) {
// Step 1
	  k = NodeNearestToTree(minDist, N);
//Step 2
	  T[i][0] = nearNode[k];
	  T[i][1] = k;
	  minDist[k] = -1;
//Step 3	  
#pragma omp parallel for
	  for (j = 1; j < N; j++) 
		  if (W[j][k] < minDist[j]) {
			  minDist[j] = W[j][k];
			  nearNode[j] = k;
		  }
  } // for i
  delete[] nearNode;
  delete[] minDist;
  return;
}


void init_data(float **W)
{
	int i,j, k;
	for (i = 0; i < V; i++) {
		for (j = 0; j < V; j++) {
			W[i][j] = FLT_MAX;
			W[j][i] = W[i][j];
		}
		W[i][i] = 0.0;
	}
	for (k = 0; k < 5*V; k++){
		i = rand() % V;
		j = rand() % V;
		if (i != j) {
			W[i][j] = (rand()%100) / 10.0; 
			W[j][i] = W[i][j];
		}
	}
}

int main(int argc, char* argv[])
{	
	float **W = new float*[V];
	int **MSTree = new int*[V];
	float val = 0.0;
	task_scheduler_init init;

	for (int i = 0; i < V; i++) {
		W[i] = new float[V];
		MSTree[i] = new int[2];
	}
	init_data(W);

	for (int i=0; i<V; i++){

		for (int j=0; j<V; j++){
			if (W[i][j] == FLT_MAX) 
				printf("*** ");
			else printf ("%3.1f ",W[i][j]);
		}
		printf("\n");
	}
	printf("\n\nEdges in MSTree:\n");

	cPrims(W, MSTree, V);

	for (int i=0; i<V-1; i++){
		int j = MSTree[i][0];
		int k = MSTree[i][1];
		val += W[j][k];
			printf ("(%d %d) ",j,k);
	}
		printf("\nVal = %5.3f\n", val);
	return 0;
}
 No newline at end of file
Loading