赞
踩
#include <pmmintrin.h> void sse_mul(int n, float a[][maxN], float b[][maxN], float c[][maxN]){ __m128 t1, t2, sum; for (int i = 0; i < n; ++i) for(int j = 0; j < i; ++j) swap(b[i][j], b[j][i]); for (int i = 0; i < n; ++i){ for (int j = 0; j < n; ++j){ c[i][j] = 0.0; sum = _mm_setzero_ps(); for (int k = n - 4; k >= 0; k -= 4){ t1 = _mm_loadu_ps(a[i] + k); t2 = _mm_loadu_ps(b[j] + k); t1 = _mm_mul_ps(t1, t2); sum = _mm_add_ps(sum, t1); } sum = _mm_hadd_ps(sum, sum); sum = _mm_hadd_ps(sum, sum); _mm_store_ss(c[i] + j, sum); for (int k = (n % 4) - 1; k >= 0; --k){ c[i][j] += a[i][k] * b[j][k]; } } } for (int i = 0; i < n; ++i) for (int j = 0; j < i; ++j) swap(b[i][j], b[j][i]); }
#include <stdio.h> #include <stdlib.h> #include <pthread.h> int thread_count; pthread_mutex_init(&mutex, NULL); void* Hello(void* rank); int main(int argc, char* argv[]){ long thread; pthread_t* thread_handles; //get number of threads from cmd thread_count = strtol(argv[1], NULL, 10); thread_handles = (pthread_t*)malloc(thread_count*sizeof(pthread_t)); for(thread = 0; thread < thread_count; thread++) pthread_create(&thread_handles[thread],NULL,Hello,(void*)thread); printf("Hello from the main thread\n"); for(thread = 0; thread < thread_count; thread++) pthread_join(thread_handles[thread],NULL); free(thread_handles); return 0; } void* Hello(void* rank) { long my_rank = (long)rank; pthread_mutex_lock(&mutex); printf("Hello from thread %ld of %d\n", my_rank, thread_count); pthread_mutex_unlock(&mutex); return NULL; }
#ifdef _OPENMP #include <omp.h> #endif void Hello(void); int main(int argc, char* argv[]) { int thread_count = strtol(argv[1],NULL,10); #pragma omp parallel num_threads(thread_count) Hello(); return 0; } void Hello(void) { #ifdef _OPENMP int my_rank = omp_get_thread_num(); int thread_count = omp_get_num_threads(); #else int my_rank = 0; int thread_count = 1; #endif printf("Hello from thread %d of %d\n",my_rank,thread_count); }
#include <mpi.h> int main(int argc, char* argv) { int myid, numprocs; int namelen; char processor_name[MPI_MAX_PROCESSOR_NAME]; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&myid);// MPI_Comm_size(MPI_COMM_WORLD,&numprocs);//这两个对称着记 MPI_Get_processor_name(processor_name,&namelen); fprintf(stderr,"Hello World! Process %d of %d on %s\n", myid, numprocs, processor_name); MPI_Finalize();// return 0; }
//省略版 int rank, a[1000], b[500]; MPI_Init(&argc, &argv);// MPI_Comm_rank(MPI_COMM_WORLD, &rank);//这个 if (rank == 0) { MPI_Send(&a[500], 500, MPI_INT, 1, 0, MPI_COMM_WORLD);//这个 sort(a, 500); MPI_Recv(b, 500, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);//和这个 } else if (rank == 1) { MPI_Recv(b, 500, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); sort(b, 500); MPI_Send(b, 500, MPI_INT, 0, 0, MPI_COMM_WORLD); } MPI_Finalize();// return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。