diff --git a/Program2/README copy.md b/Program2/README copy.md new file mode 100644 index 0000000..d7f87ef --- /dev/null +++ b/Program2/README copy.md @@ -0,0 +1,22 @@ +# Program 1 + +## Folders index + +##### /figure1_7 +- contains the first code with one shared variable and no mutex + +##### /withmutex +- contains the second version with a mutex on the variable + +##### /witharray +- contains the version using an array with an element for each thread but no padding + +##### /padded +- contains the padded version + +##### /Reports +- contains the lab report pdfs + +##### /py_notebooks +- contains the source notebooks to process data and graphs and the output PDFs + diff --git a/Program2/Reports/Test Report.pdf b/Program2/Reports/Test Report.pdf new file mode 100644 index 0000000..1e4f214 Binary files /dev/null and b/Program2/Reports/Test Report.pdf differ diff --git a/Program2/figure1_7/.clang-format b/Program2/figure1_7/.clang-format new file mode 100644 index 0000000..77cecdd --- /dev/null +++ b/Program2/figure1_7/.clang-format @@ -0,0 +1,550 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# clang-format configuration file. Intended for clang-format >= 4. +# +# For more information, see: +# +# Documentation/process/clang-format.rst +# https://clang.llvm.org/docs/ClangFormat.html +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +# +--- +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left # Unknown to clang-format-4.0 +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false # Unknown to clang-format-5.0 + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true # Unknown to clang-format-4.0 + SplitEmptyRecord: true # Unknown to clang-format-4.0 + SplitEmptyNamespace: true # Unknown to clang-format-4.0 +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false # Unknown to clang-format-4.0 +BreakBeforeTernaryOperators: false +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeComma # Unknown to clang-format-4.0 +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false # Unknown to clang-format-4.0 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false # Unknown to clang-format-4.0 + +# Taken from: +# git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' include/ \ +# | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$, - '\1'," \ +# | sort | uniq +ForEachMacros: + - 'apei_estatus_for_each_section' + - 'ata_for_each_dev' + - 'ata_for_each_link' + - '__ata_qc_for_each' + - 'ata_qc_for_each' + - 'ata_qc_for_each_raw' + - 'ata_qc_for_each_with_internal' + - 'ax25_for_each' + - 'ax25_uid_for_each' + - '__bio_for_each_bvec' + - 'bio_for_each_bvec' + - 'bio_for_each_bvec_all' + - 'bio_for_each_integrity_vec' + - '__bio_for_each_segment' + - 'bio_for_each_segment' + - 'bio_for_each_segment_all' + - 'bio_list_for_each' + - 'bip_for_each_vec' + - 'bitmap_for_each_clear_region' + - 'bitmap_for_each_set_region' + - 'blkg_for_each_descendant_post' + - 'blkg_for_each_descendant_pre' + - 'blk_queue_for_each_rl' + - 'bond_for_each_slave' + - 'bond_for_each_slave_rcu' + - 'bpf_for_each_spilled_reg' + - 'btree_for_each_safe128' + - 'btree_for_each_safe32' + - 'btree_for_each_safe64' + - 'btree_for_each_safel' + - 'card_for_each_dev' + - 'cgroup_taskset_for_each' + - 'cgroup_taskset_for_each_leader' + - 'cpufreq_for_each_entry' + - 'cpufreq_for_each_entry_idx' + - 'cpufreq_for_each_valid_entry' + - 'cpufreq_for_each_valid_entry_idx' + - 'css_for_each_child' + - 'css_for_each_descendant_post' + - 'css_for_each_descendant_pre' + - 'device_for_each_child_node' + - 'dma_fence_chain_for_each' + - 'do_for_each_ftrace_op' + - 'drm_atomic_crtc_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane_state' + - 'drm_atomic_for_each_plane_damage' + - 'drm_client_for_each_connector_iter' + - 'drm_client_for_each_modeset' + - 'drm_connector_for_each_possible_encoder' + - 'drm_for_each_bridge_in_chain' + - 'drm_for_each_connector_iter' + - 'drm_for_each_crtc' + - 'drm_for_each_crtc_reverse' + - 'drm_for_each_encoder' + - 'drm_for_each_encoder_mask' + - 'drm_for_each_fb' + - 'drm_for_each_legacy_plane' + - 'drm_for_each_plane' + - 'drm_for_each_plane_mask' + - 'drm_for_each_privobj' + - 'drm_mm_for_each_hole' + - 'drm_mm_for_each_node' + - 'drm_mm_for_each_node_in_range' + - 'drm_mm_for_each_node_safe' + - 'flow_action_for_each' + - 'for_each_active_dev_scope' + - 'for_each_active_drhd_unit' + - 'for_each_active_iommu' + - 'for_each_aggr_pgid' + - 'for_each_available_child_of_node' + - 'for_each_bio' + - 'for_each_board_func_rsrc' + - 'for_each_bvec' + - 'for_each_card_auxs' + - 'for_each_card_auxs_safe' + - 'for_each_card_components' + - 'for_each_card_dapms' + - 'for_each_card_pre_auxs' + - 'for_each_card_prelinks' + - 'for_each_card_rtds' + - 'for_each_card_rtds_safe' + - 'for_each_card_widgets' + - 'for_each_card_widgets_safe' + - 'for_each_cgroup_storage_type' + - 'for_each_child_of_node' + - 'for_each_clear_bit' + - 'for_each_clear_bit_from' + - 'for_each_cmsghdr' + - 'for_each_compatible_node' + - 'for_each_component_dais' + - 'for_each_component_dais_safe' + - 'for_each_comp_order' + - 'for_each_console' + - 'for_each_cpu' + - 'for_each_cpu_and' + - 'for_each_cpu_not' + - 'for_each_cpu_wrap' + - 'for_each_dapm_widgets' + - 'for_each_dev_addr' + - 'for_each_dev_scope' + - 'for_each_displayid_db' + - 'for_each_dma_cap_mask' + - 'for_each_dpcm_be' + - 'for_each_dpcm_be_rollback' + - 'for_each_dpcm_be_safe' + - 'for_each_dpcm_fe' + - 'for_each_drhd_unit' + - 'for_each_dss_dev' + - 'for_each_efi_memory_desc' + - 'for_each_efi_memory_desc_in_map' + - 'for_each_element' + - 'for_each_element_extid' + - 'for_each_element_id' + - 'for_each_endpoint_of_node' + - 'for_each_evictable_lru' + - 'for_each_fib6_node_rt_rcu' + - 'for_each_fib6_walker_rt' + - 'for_each_free_mem_pfn_range_in_zone' + - 'for_each_free_mem_pfn_range_in_zone_from' + - 'for_each_free_mem_range' + - 'for_each_free_mem_range_reverse' + - 'for_each_func_rsrc' + - 'for_each_hstate' + - 'for_each_if' + - 'for_each_iommu' + - 'for_each_ip_tunnel_rcu' + - 'for_each_irq_nr' + - 'for_each_link_codecs' + - 'for_each_link_cpus' + - 'for_each_link_platforms' + - 'for_each_lru' + - 'for_each_matching_node' + - 'for_each_matching_node_and_match' + - 'for_each_member' + - 'for_each_memcg_cache_index' + - 'for_each_mem_pfn_range' + - '__for_each_mem_range' + - 'for_each_mem_range' + - '__for_each_mem_range_rev' + - 'for_each_mem_range_rev' + - 'for_each_mem_region' + - 'for_each_migratetype_order' + - 'for_each_msi_entry' + - 'for_each_msi_entry_safe' + - 'for_each_net' + - 'for_each_net_continue_reverse' + - 'for_each_netdev' + - 'for_each_netdev_continue' + - 'for_each_netdev_continue_rcu' + - 'for_each_netdev_continue_reverse' + - 'for_each_netdev_feature' + - 'for_each_netdev_in_bond_rcu' + - 'for_each_netdev_rcu' + - 'for_each_netdev_reverse' + - 'for_each_netdev_safe' + - 'for_each_net_rcu' + - 'for_each_new_connector_in_state' + - 'for_each_new_crtc_in_state' + - 'for_each_new_mst_mgr_in_state' + - 'for_each_new_plane_in_state' + - 'for_each_new_private_obj_in_state' + - 'for_each_node' + - 'for_each_node_by_name' + - 'for_each_node_by_type' + - 'for_each_node_mask' + - 'for_each_node_state' + - 'for_each_node_with_cpus' + - 'for_each_node_with_property' + - 'for_each_nonreserved_multicast_dest_pgid' + - 'for_each_of_allnodes' + - 'for_each_of_allnodes_from' + - 'for_each_of_cpu_node' + - 'for_each_of_pci_range' + - 'for_each_old_connector_in_state' + - 'for_each_old_crtc_in_state' + - 'for_each_old_mst_mgr_in_state' + - 'for_each_oldnew_connector_in_state' + - 'for_each_oldnew_crtc_in_state' + - 'for_each_oldnew_mst_mgr_in_state' + - 'for_each_oldnew_plane_in_state' + - 'for_each_oldnew_plane_in_state_reverse' + - 'for_each_oldnew_private_obj_in_state' + - 'for_each_old_plane_in_state' + - 'for_each_old_private_obj_in_state' + - 'for_each_online_cpu' + - 'for_each_online_node' + - 'for_each_online_pgdat' + - 'for_each_pci_bridge' + - 'for_each_pci_dev' + - 'for_each_pci_msi_entry' + - 'for_each_pcm_streams' + - 'for_each_physmem_range' + - 'for_each_populated_zone' + - 'for_each_possible_cpu' + - 'for_each_present_cpu' + - 'for_each_prime_number' + - 'for_each_prime_number_from' + - 'for_each_process' + - 'for_each_process_thread' + - 'for_each_property_of_node' + - 'for_each_registered_fb' + - 'for_each_requested_gpio' + - 'for_each_requested_gpio_in_range' + - 'for_each_reserved_mem_range' + - 'for_each_reserved_mem_region' + - 'for_each_rtd_codec_dais' + - 'for_each_rtd_components' + - 'for_each_rtd_cpu_dais' + - 'for_each_rtd_dais' + - 'for_each_set_bit' + - 'for_each_set_bit_from' + - 'for_each_set_clump8' + - 'for_each_sg' + - 'for_each_sg_dma_page' + - 'for_each_sg_page' + - 'for_each_sgtable_dma_page' + - 'for_each_sgtable_dma_sg' + - 'for_each_sgtable_page' + - 'for_each_sgtable_sg' + - 'for_each_sibling_event' + - 'for_each_subelement' + - 'for_each_subelement_extid' + - 'for_each_subelement_id' + - '__for_each_thread' + - 'for_each_thread' + - 'for_each_unicast_dest_pgid' + - 'for_each_vsi' + - 'for_each_wakeup_source' + - 'for_each_zone' + - 'for_each_zone_zonelist' + - 'for_each_zone_zonelist_nodemask' + - 'fwnode_for_each_available_child_node' + - 'fwnode_for_each_child_node' + - 'fwnode_graph_for_each_endpoint' + - 'gadget_for_each_ep' + - 'genradix_for_each' + - 'genradix_for_each_from' + - 'hash_for_each' + - 'hash_for_each_possible' + - 'hash_for_each_possible_rcu' + - 'hash_for_each_possible_rcu_notrace' + - 'hash_for_each_possible_safe' + - 'hash_for_each_rcu' + - 'hash_for_each_safe' + - 'hctx_for_each_ctx' + - 'hlist_bl_for_each_entry' + - 'hlist_bl_for_each_entry_rcu' + - 'hlist_bl_for_each_entry_safe' + - 'hlist_for_each' + - 'hlist_for_each_entry' + - 'hlist_for_each_entry_continue' + - 'hlist_for_each_entry_continue_rcu' + - 'hlist_for_each_entry_continue_rcu_bh' + - 'hlist_for_each_entry_from' + - 'hlist_for_each_entry_from_rcu' + - 'hlist_for_each_entry_rcu' + - 'hlist_for_each_entry_rcu_bh' + - 'hlist_for_each_entry_rcu_notrace' + - 'hlist_for_each_entry_safe' + - 'hlist_for_each_entry_srcu' + - '__hlist_for_each_rcu' + - 'hlist_for_each_safe' + - 'hlist_nulls_for_each_entry' + - 'hlist_nulls_for_each_entry_from' + - 'hlist_nulls_for_each_entry_rcu' + - 'hlist_nulls_for_each_entry_safe' + - 'i3c_bus_for_each_i2cdev' + - 'i3c_bus_for_each_i3cdev' + - 'ide_host_for_each_port' + - 'ide_port_for_each_dev' + - 'ide_port_for_each_present_dev' + - 'idr_for_each_entry' + - 'idr_for_each_entry_continue' + - 'idr_for_each_entry_continue_ul' + - 'idr_for_each_entry_ul' + - 'in_dev_for_each_ifa_rcu' + - 'in_dev_for_each_ifa_rtnl' + - 'inet_bind_bucket_for_each' + - 'inet_lhash2_for_each_icsk_rcu' + - 'key_for_each' + - 'key_for_each_safe' + - 'klp_for_each_func' + - 'klp_for_each_func_safe' + - 'klp_for_each_func_static' + - 'klp_for_each_object' + - 'klp_for_each_object_safe' + - 'klp_for_each_object_static' + - 'kunit_suite_for_each_test_case' + - 'kvm_for_each_memslot' + - 'kvm_for_each_vcpu' + - 'list_for_each' + - 'list_for_each_codec' + - 'list_for_each_codec_safe' + - 'list_for_each_continue' + - 'list_for_each_entry' + - 'list_for_each_entry_continue' + - 'list_for_each_entry_continue_rcu' + - 'list_for_each_entry_continue_reverse' + - 'list_for_each_entry_from' + - 'list_for_each_entry_from_rcu' + - 'list_for_each_entry_from_reverse' + - 'list_for_each_entry_lockless' + - 'list_for_each_entry_rcu' + - 'list_for_each_entry_reverse' + - 'list_for_each_entry_safe' + - 'list_for_each_entry_safe_continue' + - 'list_for_each_entry_safe_from' + - 'list_for_each_entry_safe_reverse' + - 'list_for_each_entry_srcu' + - 'list_for_each_prev' + - 'list_for_each_prev_safe' + - 'list_for_each_safe' + - 'llist_for_each' + - 'llist_for_each_entry' + - 'llist_for_each_entry_safe' + - 'llist_for_each_safe' + - 'mci_for_each_dimm' + - 'media_device_for_each_entity' + - 'media_device_for_each_intf' + - 'media_device_for_each_link' + - 'media_device_for_each_pad' + - 'nanddev_io_for_each_page' + - 'netdev_for_each_lower_dev' + - 'netdev_for_each_lower_private' + - 'netdev_for_each_lower_private_rcu' + - 'netdev_for_each_mc_addr' + - 'netdev_for_each_uc_addr' + - 'netdev_for_each_upper_dev_rcu' + - 'netdev_hw_addr_list_for_each' + - 'nft_rule_for_each_expr' + - 'nla_for_each_attr' + - 'nla_for_each_nested' + - 'nlmsg_for_each_attr' + - 'nlmsg_for_each_msg' + - 'nr_neigh_for_each' + - 'nr_neigh_for_each_safe' + - 'nr_node_for_each' + - 'nr_node_for_each_safe' + - 'of_for_each_phandle' + - 'of_property_for_each_string' + - 'of_property_for_each_u32' + - 'pci_bus_for_each_resource' + - 'pcl_for_each_chunk' + - 'pcl_for_each_segment' + - 'pcm_for_each_format' + - 'ping_portaddr_for_each_entry' + - 'plist_for_each' + - 'plist_for_each_continue' + - 'plist_for_each_entry' + - 'plist_for_each_entry_continue' + - 'plist_for_each_entry_safe' + - 'plist_for_each_safe' + - 'pnp_for_each_card' + - 'pnp_for_each_dev' + - 'protocol_for_each_card' + - 'protocol_for_each_dev' + - 'queue_for_each_hw_ctx' + - 'radix_tree_for_each_slot' + - 'radix_tree_for_each_tagged' + - 'rbtree_postorder_for_each_entry_safe' + - 'rdma_for_each_block' + - 'rdma_for_each_port' + - 'rdma_umem_for_each_dma_block' + - 'resource_list_for_each_entry' + - 'resource_list_for_each_entry_safe' + - 'rhl_for_each_entry_rcu' + - 'rhl_for_each_rcu' + - 'rht_for_each' + - 'rht_for_each_entry' + - 'rht_for_each_entry_from' + - 'rht_for_each_entry_rcu' + - 'rht_for_each_entry_rcu_from' + - 'rht_for_each_entry_safe' + - 'rht_for_each_from' + - 'rht_for_each_rcu' + - 'rht_for_each_rcu_from' + - '__rq_for_each_bio' + - 'rq_for_each_bvec' + - 'rq_for_each_segment' + - 'scsi_for_each_prot_sg' + - 'scsi_for_each_sg' + - 'sctp_for_each_hentry' + - 'sctp_skb_for_each' + - 'shdma_for_each_chan' + - '__shost_for_each_device' + - 'shost_for_each_device' + - 'sk_for_each' + - 'sk_for_each_bound' + - 'sk_for_each_entry_offset_rcu' + - 'sk_for_each_from' + - 'sk_for_each_rcu' + - 'sk_for_each_safe' + - 'sk_nulls_for_each' + - 'sk_nulls_for_each_from' + - 'sk_nulls_for_each_rcu' + - 'snd_array_for_each' + - 'snd_pcm_group_for_each_entry' + - 'snd_soc_dapm_widget_for_each_path' + - 'snd_soc_dapm_widget_for_each_path_safe' + - 'snd_soc_dapm_widget_for_each_sink_path' + - 'snd_soc_dapm_widget_for_each_source_path' + - 'tb_property_for_each' + - 'tcf_exts_for_each_action' + - 'udp_portaddr_for_each_entry' + - 'udp_portaddr_for_each_entry_rcu' + - 'usb_hub_for_each_child' + - 'v4l2_device_for_each_subdev' + - 'v4l2_m2m_for_each_dst_buf' + - 'v4l2_m2m_for_each_dst_buf_safe' + - 'v4l2_m2m_for_each_src_buf' + - 'v4l2_m2m_for_each_src_buf_safe' + - 'virtio_device_for_each_vq' + - 'while_for_each_ftrace_op' + - 'xa_for_each' + - 'xa_for_each_marked' + - 'xa_for_each_range' + - 'xa_for_each_start' + - 'xas_for_each' + - 'xas_for_each_conflict' + - 'xas_for_each_marked' + - 'xbc_array_for_each_value' + - 'xbc_for_each_key_value' + - 'xbc_node_for_each_array_value' + - 'xbc_node_for_each_child' + - 'xbc_node_for_each_key_value' + - 'zorro_for_each_dev' + +IncludeBlocks: Preserve # Unknown to clang-format-5.0 +IncludeCategories: + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None # Unknown to clang-format-5.0 +IndentWidth: 8 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto # Unknown to clang-format-5.0 +ObjCBlockIndentWidth: 8 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true + +# Taken from git's rules +PenaltyBreakAssignment: 10 # Unknown to clang-format-4.0 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +PointerAlignment: Right +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false # Unknown to clang-format-4.0 +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCtorInitializerColon: true # Unknown to clang-format-5.0 +SpaceBeforeInheritanceColon: true # Unknown to clang-format-5.0 +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true # Unknown to clang-format-5.0 +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp03 +TabWidth: 8 +UseTab: false \ No newline at end of file diff --git a/Program2/figure1_7/Makefile b/Program2/figure1_7/Makefile new file mode 100644 index 0000000..c30fc1b --- /dev/null +++ b/Program2/figure1_7/Makefile @@ -0,0 +1,7 @@ +all: figure1_7 + +figure1_7: figure1_7.c + gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -o figure1_7 figure1_7.c + +clean: + rm -rf figure1_7 diff --git a/Program2/figure1_7/figure1_7.c b/Program2/figure1_7/figure1_7.c new file mode 100644 index 0000000..40d17e0 --- /dev/null +++ b/Program2/figure1_7/figure1_7.c @@ -0,0 +1,295 @@ +// Ziad Arafat +// Created: Aug 31 2021 +// Figure 1.7 in the textbook + +/** + * @brief This program will create an array of a specified size and fill it + * with random numbers between 1 and 3 inclusive. It will then try to count + * the number of threes in the array using a serial method and a parallel method + * it will return the counts for each attempt as well as the running times. + * + * This version will use a single variable shared by all the threads without + * uring a mutex. We expect it to return incorrect results + * + */ + +/** +Changelog +09/30 +- Copied code from Program 1 +- Copied and refactored shared memory code from program 1.5 +- Modified the thread to use the shared memory +*/ + +#define DEBUG 0 // flag to print debug values + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// create key for shared memory +#define SHMKEY ((key_t)1497) + +int *A; // Pointer to our array in heap + +// struct to store shared memory count +typedef struct { + int count; +} shmem_count; + +// initialize memory +static shmem_count *COUNT; + +int SEGSIZE; // the size of each chunk per tread +int NUMOFTHREADS; // the number of threads to spawn +int SIZE; // The size of the input array + +/** + * @brief Iterates through a chunk of the A array and counts the number of + * threes by updating the COUNT variable + * + * + * @param index The index of the array chunks re are working with + * @return void* returns a void pointer to work with pthread + * + * @pre The global A variable must be a pointer to an array in heap containing numbers between + * 1 and 3. index must be a number referring to the index of the chunk we want + * to work through starting at 0. The global COUNT variable must contain the + * number of threes we have already counted + * + * @post When done the COUNT variable must be updated to have the number of + * threes found in our chunk of the array + */ +void *count3s(void *idx) +{ + // if (DEBUG) + // printf("I ran once %d\n", index); + + int *index = (int *)idx; // cast the void star input to an int ptr + int mystart = *index * SEGSIZE; // set the starting point for the thread + int myend = mystart + SEGSIZE; // set the end point for the thread + + if (DEBUG) { + printf("start is %d\n", mystart); + printf("end is %d\n", myend); + } + + // iterate through our section of the array and count the threes + for (int i = mystart; i < myend; i++) { + if (A[i] == 3) { + if (DEBUG) + printf("Found a three\n"); + COUNT->count++; + } + } + + // When we reach the final index we will calculate the remaining values in the array + // So that we can add them to the result. + // This can become quite high once you go past the 50% mark of the array size + // but our tests will never go that far so this is a pretty good solution. + if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) { + if (DEBUG) { + int remain = SIZE - myend; + printf("Remainder: %d \n", remain); + } + + // printf("ACTIVE \n"); + + // iterate through the remainder of the array and count the 3s + for (int i = myend; i < SIZE; i++) { + if (A[i] == 3) { + COUNT->count++; // increment count + } + } + + if (DEBUG) { + printf("The count is now %d threads %d index %d SIZE %d end %d \n", + COUNT->count, NUMOFTHREADS, *index, SIZE, myend); + } + } + + return (void *)0; +} + +/** + * @brief This function is used to spawn the appropriate number of threads as + * well as keep track of their progress. + * + * @return int containing the time it took + * + * @pre The global COUNT variable is 0. NUMOFTHREADS is greater than 0. + * SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable + * points to an array in heap. + * + * @post The global COUNT contains the correct three count + * + */ +int count3s_parallel() +{ + // initialize the timer for the parallel work + struct timespec starttime, endtime; + int j, k; + + // get the start time + clock_gettime(CLOCK_REALTIME, &starttime); // start the timer + + // Created an array allocated at runtime to hold the identifiers for + // our threads + pthread_t *t_idents; + int *t_indices; + + // Allocates the space needed for the thread IDs + t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS)); + + // in order to fix the "integer to pointer conversion" warning + // i made an array to store the indices for each thread that way + // i can pass it as a pointer in the for loop + t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS)); + + // Create the threads + for (int i = 0; i < NUMOFTHREADS; i++) { + t_indices[i] = i; + pthread_create(&t_idents[i], NULL, count3s, + (void *)&t_indices[i]); + } + + // wait for all the treads to finish. + for (int i = 0; i < NUMOFTHREADS; i++) { + pthread_join(t_idents[i], NULL); + } + + // free the ids and indexes in memory so we dont leak + free(t_idents); + free(t_indices); + + // get the end time + clock_gettime(CLOCK_REALTIME, &endtime); + + // calculate duration + j = endtime.tv_sec - starttime.tv_sec; + k = endtime.tv_nsec - starttime.tv_nsec; + j = j * 1000000000 + k; + + // return the duration + return j; +} + +int main(int argc, char const *argv[]) +{ + int shmid; // The shared memory id + + char *shmadd_ptr; // The address of the shared memory + shmadd_ptr = (char *)0; + + if ((shmid = shmget(SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0) { + perror("uh oh, shmget failed"); + exit(1); + } + + // check if the shared mem attached correctly. + if ((COUNT = (shmem_count *)shmat(shmid, shmadd_ptr, 0)) == + (shmem_count *)-1) { + perror("shmat failed"); + exit(1); + } + + // this variable stores the time for generating the random numbers. + time_t t; + + // intitilize the random number with the time as the seed + srand((unsigned)time(&t)); + + // check if the correct arguments are used + if (argc != 3) { + // if they are wrong barf and exit + printf("Not correct arguments \n"); + printf("Usage %s \n", + argv[0]); + exit(1); + } + + // Convert the user input to integers + SIZE = atoi(argv[1]); + NUMOFTHREADS = atoi(argv[2]); + + // Set the size of the array chunks to process to the size over the + // number of threads + SEGSIZE = SIZE / NUMOFTHREADS; + + if (DEBUG) { + printf("number is %s \n", argv[1]); + printf("number is %d \n", SIZE); + printf("NumThr is %d \n", NUMOFTHREADS); + printf("Seg Size is %d \n", SEGSIZE); + } + + // Allocate the array based on the input size + A = (int *)(malloc(sizeof(int) * SIZE)); + + // if the array is null barf + if (A == NULL) { + printf("Austin, we have a problem"); + exit(1); + } + + // generate random numbers and fill them in the array + // the numbers are between 1 and 3 inclusive + for (int i = 0; i < SIZE; i++) { + A[i] = rand() % 4; + // printf("E%d \n", A[i]); + } + + // Count the threes in parallel + int parallel_time = count3s_parallel(); + + // initialize the count for the serial code + int local_count = 0; + + // initialize the timer for the parallel work + struct timespec starttime, endtime; + int j, k; + + clock_gettime(CLOCK_REALTIME, &starttime); // start the timer + + // Count the threes in serial + for (int i = 0; i < SIZE; i++) { + if (A[i] == 3) { + local_count++; + } + } + + // get end time + clock_gettime(CLOCK_REALTIME, &endtime); + + // calculate duration + j = endtime.tv_sec - starttime.tv_sec; + k = endtime.tv_nsec - starttime.tv_nsec; + j = j * 1000000000 + k; + + // free the array + free(A); + + // print the times and counts + printf("Time parallel: %d \n", parallel_time); + printf("Time serial: %d \n", j); + + printf("parallel count is %d\n", COUNT->count); + printf("serial count is %d\n", local_count); + + // Delete the shared memory. If it fails barf + if (shmdt(COUNT) == -1) { // clean up the shared memory + + // barf + perror("Cant delete the shared memory"); + exit(-1); + } + + return 0; +} diff --git a/Program2/outs.txt b/Program2/outs.txt new file mode 100644 index 0000000..6214916 --- /dev/null +++ b/Program2/outs.txt @@ -0,0 +1,257 @@ +#### Raw console outputs + +##### 1 Thread + +```bash +doge@godemperordoge1998 ~/D/p/padded (main)> make +gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o padded padded.c +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1 +Time parallel: 79589173 +Time serial: 76582925 +parallel count is 2499295 +serial count is 2499295 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1 +Time parallel: 77817021 +Time serial: 76191186 +parallel count is 2501600 +serial count is 2501600 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1 +Time parallel: 78369396 +Time serial: 76075458 +parallel count is 2501212 +serial count is 2501212 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1 +Time parallel: 77878131 +Time serial: 75789878 +parallel count is 2497788 +serial count is 2497788 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1 +Time parallel: 78617891 +Time serial: 76074550 +parallel count is 2499979 +serial count is 2499979 +doge@godemperordoge1998 ~/D/p/padded (main)> +``` + +##### 2 Threads + +```bash +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2 +Time parallel: 39898720 +Time serial: 76881427 +parallel count is 2498665 +serial count is 2498665 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2 +Time parallel: 38993717 +Time serial: 75975306 +parallel count is 2498665 +serial count is 2498665 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2 +Time parallel: 43756962 +Time serial: 75922017 +parallel count is 2499470 +serial count is 2499470 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2 +Time parallel: 39628155 +Time serial: 75874106 +parallel count is 2499470 +serial count is 2499470 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2 +Time parallel: 38768479 +Time serial: 83936863 +parallel count is 2499249 +serial count is 2499249 +doge@godemperordoge1998 ~/D/p/padded (main)> + + +``` + +##### 4 Threads +```bash +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4 +Time parallel: 20135731 +Time serial: 76379757 +parallel count is 2500818 +serial count is 2500818 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4 +Time parallel: 20184131 +Time serial: 76066309 +parallel count is 2500619 +serial count is 2500619 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4 +Time parallel: 20012671 +Time serial: 76159618 +parallel count is 2500619 +serial count is 2500619 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4 +Time parallel: 20407135 +Time serial: 76091592 +parallel count is 2501896 +serial count is 2501896 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4 +Time parallel: 19932075 +Time serial: 75735192 +parallel count is 2501896 +serial count is 2501896 +doge@godemperordoge1998 ~/D/p/padded (main)> + +``` + +##### 8 Threads +```bash +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8 +Time parallel: 10802910 +Time serial: 76379477 +parallel count is 2499488 +serial count is 2499488 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8 +Time parallel: 10938262 +Time serial: 75819630 +parallel count is 2499488 +serial count is 2499488 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8 +Time parallel: 10780422 +Time serial: 75986271 +parallel count is 2499970 +serial count is 2499970 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8 +Time parallel: 10738028 +Time serial: 75738055 +parallel count is 2499970 +serial count is 2499970 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8 +Time parallel: 10694446 +Time serial: 76413770 +parallel count is 2498866 +serial count is 2498866 +doge@godemperordoge1998 ~/D/p/padded (main)> +``` + +##### 16 Threads + +```bash +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16 +Time parallel: 15428848 +Time serial: 76461611 +parallel count is 2499356 +serial count is 2499356 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16 +Time parallel: 10718332 +Time serial: 76038093 +parallel count is 2497083 +serial count is 2497083 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16 +Time parallel: 11129069 +Time serial: 76266824 +parallel count is 2497083 +serial count is 2497083 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16 +Time parallel: 12962264 +Time serial: 76701516 +parallel count is 2500337 +serial count is 2500337 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16 +Time parallel: 12169984 +Time serial: 76016583 +parallel count is 2500337 +serial count is 2500337 +doge@godemperordoge1998 ~/D/p/padded (main)> + +``` + +##### 32 Threads + +```bash +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32 +Time parallel: 13191343 +Time serial: 76977528 +parallel count is 2501573 +serial count is 2501573 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32 +Time parallel: 11433019 +Time serial: 75182119 +parallel count is 2499230 +serial count is 2499230 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32 +Time parallel: 11259742 +Time serial: 76361110 +parallel count is 2499230 +serial count is 2499230 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32 +Time parallel: 13108860 +Time serial: 76463636 +parallel count is 2497643 +serial count is 2497643 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32 +Time parallel: 12005927 +Time serial: 76738602 +parallel count is 2500608 +serial count is 2500608 +doge@godemperordoge1998 ~/D/p/padded (main)> + + + +``` + +##### 64 Threads + +```bash +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64 +Time parallel: 13443889 +Time serial: 75998703 +parallel count is 2501406 +serial count is 2501406 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64 +Time parallel: 13066536 +Time serial: 80285631 +parallel count is 2498365 +serial count is 2498365 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64 +Time parallel: 12961145 +Time serial: 76057230 +parallel count is 2497858 +serial count is 2497858 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64 +Time parallel: 13297153 +Time serial: 76248665 +parallel count is 2497858 +serial count is 2497858 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64 +Time parallel: 12780816 +Time serial: 75720945 +parallel count is 2500354 +serial count is 2500354 +doge@godemperordoge1998 ~/D/p/padded (main)> +``` + +##### 128 Threads + +```bash +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128 +Time parallel: 14702009 +Time serial: 75864468 +parallel count is 2497320 +serial count is 2497320 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128 +Time parallel: 15708353 +Time serial: 76406018 +parallel count is 2497320 +serial count is 2497320 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128 +Time parallel: 14599762 +Time serial: 76125185 +parallel count is 2499108 +serial count is 2499108 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128 +Time parallel: 18146093 +Time serial: 76192373 +parallel count is 2496782 +serial count is 2496782 +doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128 +Time parallel: 15152136 +Time serial: 76688875 +parallel count is 2496782 +serial count is 2496782 +doge@godemperordoge1998 ~/D/p/padded (main)> + +``` \ No newline at end of file diff --git a/Program2/padded/.clang-format b/Program2/padded/.clang-format new file mode 100644 index 0000000..77cecdd --- /dev/null +++ b/Program2/padded/.clang-format @@ -0,0 +1,550 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# clang-format configuration file. Intended for clang-format >= 4. +# +# For more information, see: +# +# Documentation/process/clang-format.rst +# https://clang.llvm.org/docs/ClangFormat.html +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +# +--- +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left # Unknown to clang-format-4.0 +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false # Unknown to clang-format-5.0 + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true # Unknown to clang-format-4.0 + SplitEmptyRecord: true # Unknown to clang-format-4.0 + SplitEmptyNamespace: true # Unknown to clang-format-4.0 +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false # Unknown to clang-format-4.0 +BreakBeforeTernaryOperators: false +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeComma # Unknown to clang-format-4.0 +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false # Unknown to clang-format-4.0 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false # Unknown to clang-format-4.0 + +# Taken from: +# git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' include/ \ +# | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$, - '\1'," \ +# | sort | uniq +ForEachMacros: + - 'apei_estatus_for_each_section' + - 'ata_for_each_dev' + - 'ata_for_each_link' + - '__ata_qc_for_each' + - 'ata_qc_for_each' + - 'ata_qc_for_each_raw' + - 'ata_qc_for_each_with_internal' + - 'ax25_for_each' + - 'ax25_uid_for_each' + - '__bio_for_each_bvec' + - 'bio_for_each_bvec' + - 'bio_for_each_bvec_all' + - 'bio_for_each_integrity_vec' + - '__bio_for_each_segment' + - 'bio_for_each_segment' + - 'bio_for_each_segment_all' + - 'bio_list_for_each' + - 'bip_for_each_vec' + - 'bitmap_for_each_clear_region' + - 'bitmap_for_each_set_region' + - 'blkg_for_each_descendant_post' + - 'blkg_for_each_descendant_pre' + - 'blk_queue_for_each_rl' + - 'bond_for_each_slave' + - 'bond_for_each_slave_rcu' + - 'bpf_for_each_spilled_reg' + - 'btree_for_each_safe128' + - 'btree_for_each_safe32' + - 'btree_for_each_safe64' + - 'btree_for_each_safel' + - 'card_for_each_dev' + - 'cgroup_taskset_for_each' + - 'cgroup_taskset_for_each_leader' + - 'cpufreq_for_each_entry' + - 'cpufreq_for_each_entry_idx' + - 'cpufreq_for_each_valid_entry' + - 'cpufreq_for_each_valid_entry_idx' + - 'css_for_each_child' + - 'css_for_each_descendant_post' + - 'css_for_each_descendant_pre' + - 'device_for_each_child_node' + - 'dma_fence_chain_for_each' + - 'do_for_each_ftrace_op' + - 'drm_atomic_crtc_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane_state' + - 'drm_atomic_for_each_plane_damage' + - 'drm_client_for_each_connector_iter' + - 'drm_client_for_each_modeset' + - 'drm_connector_for_each_possible_encoder' + - 'drm_for_each_bridge_in_chain' + - 'drm_for_each_connector_iter' + - 'drm_for_each_crtc' + - 'drm_for_each_crtc_reverse' + - 'drm_for_each_encoder' + - 'drm_for_each_encoder_mask' + - 'drm_for_each_fb' + - 'drm_for_each_legacy_plane' + - 'drm_for_each_plane' + - 'drm_for_each_plane_mask' + - 'drm_for_each_privobj' + - 'drm_mm_for_each_hole' + - 'drm_mm_for_each_node' + - 'drm_mm_for_each_node_in_range' + - 'drm_mm_for_each_node_safe' + - 'flow_action_for_each' + - 'for_each_active_dev_scope' + - 'for_each_active_drhd_unit' + - 'for_each_active_iommu' + - 'for_each_aggr_pgid' + - 'for_each_available_child_of_node' + - 'for_each_bio' + - 'for_each_board_func_rsrc' + - 'for_each_bvec' + - 'for_each_card_auxs' + - 'for_each_card_auxs_safe' + - 'for_each_card_components' + - 'for_each_card_dapms' + - 'for_each_card_pre_auxs' + - 'for_each_card_prelinks' + - 'for_each_card_rtds' + - 'for_each_card_rtds_safe' + - 'for_each_card_widgets' + - 'for_each_card_widgets_safe' + - 'for_each_cgroup_storage_type' + - 'for_each_child_of_node' + - 'for_each_clear_bit' + - 'for_each_clear_bit_from' + - 'for_each_cmsghdr' + - 'for_each_compatible_node' + - 'for_each_component_dais' + - 'for_each_component_dais_safe' + - 'for_each_comp_order' + - 'for_each_console' + - 'for_each_cpu' + - 'for_each_cpu_and' + - 'for_each_cpu_not' + - 'for_each_cpu_wrap' + - 'for_each_dapm_widgets' + - 'for_each_dev_addr' + - 'for_each_dev_scope' + - 'for_each_displayid_db' + - 'for_each_dma_cap_mask' + - 'for_each_dpcm_be' + - 'for_each_dpcm_be_rollback' + - 'for_each_dpcm_be_safe' + - 'for_each_dpcm_fe' + - 'for_each_drhd_unit' + - 'for_each_dss_dev' + - 'for_each_efi_memory_desc' + - 'for_each_efi_memory_desc_in_map' + - 'for_each_element' + - 'for_each_element_extid' + - 'for_each_element_id' + - 'for_each_endpoint_of_node' + - 'for_each_evictable_lru' + - 'for_each_fib6_node_rt_rcu' + - 'for_each_fib6_walker_rt' + - 'for_each_free_mem_pfn_range_in_zone' + - 'for_each_free_mem_pfn_range_in_zone_from' + - 'for_each_free_mem_range' + - 'for_each_free_mem_range_reverse' + - 'for_each_func_rsrc' + - 'for_each_hstate' + - 'for_each_if' + - 'for_each_iommu' + - 'for_each_ip_tunnel_rcu' + - 'for_each_irq_nr' + - 'for_each_link_codecs' + - 'for_each_link_cpus' + - 'for_each_link_platforms' + - 'for_each_lru' + - 'for_each_matching_node' + - 'for_each_matching_node_and_match' + - 'for_each_member' + - 'for_each_memcg_cache_index' + - 'for_each_mem_pfn_range' + - '__for_each_mem_range' + - 'for_each_mem_range' + - '__for_each_mem_range_rev' + - 'for_each_mem_range_rev' + - 'for_each_mem_region' + - 'for_each_migratetype_order' + - 'for_each_msi_entry' + - 'for_each_msi_entry_safe' + - 'for_each_net' + - 'for_each_net_continue_reverse' + - 'for_each_netdev' + - 'for_each_netdev_continue' + - 'for_each_netdev_continue_rcu' + - 'for_each_netdev_continue_reverse' + - 'for_each_netdev_feature' + - 'for_each_netdev_in_bond_rcu' + - 'for_each_netdev_rcu' + - 'for_each_netdev_reverse' + - 'for_each_netdev_safe' + - 'for_each_net_rcu' + - 'for_each_new_connector_in_state' + - 'for_each_new_crtc_in_state' + - 'for_each_new_mst_mgr_in_state' + - 'for_each_new_plane_in_state' + - 'for_each_new_private_obj_in_state' + - 'for_each_node' + - 'for_each_node_by_name' + - 'for_each_node_by_type' + - 'for_each_node_mask' + - 'for_each_node_state' + - 'for_each_node_with_cpus' + - 'for_each_node_with_property' + - 'for_each_nonreserved_multicast_dest_pgid' + - 'for_each_of_allnodes' + - 'for_each_of_allnodes_from' + - 'for_each_of_cpu_node' + - 'for_each_of_pci_range' + - 'for_each_old_connector_in_state' + - 'for_each_old_crtc_in_state' + - 'for_each_old_mst_mgr_in_state' + - 'for_each_oldnew_connector_in_state' + - 'for_each_oldnew_crtc_in_state' + - 'for_each_oldnew_mst_mgr_in_state' + - 'for_each_oldnew_plane_in_state' + - 'for_each_oldnew_plane_in_state_reverse' + - 'for_each_oldnew_private_obj_in_state' + - 'for_each_old_plane_in_state' + - 'for_each_old_private_obj_in_state' + - 'for_each_online_cpu' + - 'for_each_online_node' + - 'for_each_online_pgdat' + - 'for_each_pci_bridge' + - 'for_each_pci_dev' + - 'for_each_pci_msi_entry' + - 'for_each_pcm_streams' + - 'for_each_physmem_range' + - 'for_each_populated_zone' + - 'for_each_possible_cpu' + - 'for_each_present_cpu' + - 'for_each_prime_number' + - 'for_each_prime_number_from' + - 'for_each_process' + - 'for_each_process_thread' + - 'for_each_property_of_node' + - 'for_each_registered_fb' + - 'for_each_requested_gpio' + - 'for_each_requested_gpio_in_range' + - 'for_each_reserved_mem_range' + - 'for_each_reserved_mem_region' + - 'for_each_rtd_codec_dais' + - 'for_each_rtd_components' + - 'for_each_rtd_cpu_dais' + - 'for_each_rtd_dais' + - 'for_each_set_bit' + - 'for_each_set_bit_from' + - 'for_each_set_clump8' + - 'for_each_sg' + - 'for_each_sg_dma_page' + - 'for_each_sg_page' + - 'for_each_sgtable_dma_page' + - 'for_each_sgtable_dma_sg' + - 'for_each_sgtable_page' + - 'for_each_sgtable_sg' + - 'for_each_sibling_event' + - 'for_each_subelement' + - 'for_each_subelement_extid' + - 'for_each_subelement_id' + - '__for_each_thread' + - 'for_each_thread' + - 'for_each_unicast_dest_pgid' + - 'for_each_vsi' + - 'for_each_wakeup_source' + - 'for_each_zone' + - 'for_each_zone_zonelist' + - 'for_each_zone_zonelist_nodemask' + - 'fwnode_for_each_available_child_node' + - 'fwnode_for_each_child_node' + - 'fwnode_graph_for_each_endpoint' + - 'gadget_for_each_ep' + - 'genradix_for_each' + - 'genradix_for_each_from' + - 'hash_for_each' + - 'hash_for_each_possible' + - 'hash_for_each_possible_rcu' + - 'hash_for_each_possible_rcu_notrace' + - 'hash_for_each_possible_safe' + - 'hash_for_each_rcu' + - 'hash_for_each_safe' + - 'hctx_for_each_ctx' + - 'hlist_bl_for_each_entry' + - 'hlist_bl_for_each_entry_rcu' + - 'hlist_bl_for_each_entry_safe' + - 'hlist_for_each' + - 'hlist_for_each_entry' + - 'hlist_for_each_entry_continue' + - 'hlist_for_each_entry_continue_rcu' + - 'hlist_for_each_entry_continue_rcu_bh' + - 'hlist_for_each_entry_from' + - 'hlist_for_each_entry_from_rcu' + - 'hlist_for_each_entry_rcu' + - 'hlist_for_each_entry_rcu_bh' + - 'hlist_for_each_entry_rcu_notrace' + - 'hlist_for_each_entry_safe' + - 'hlist_for_each_entry_srcu' + - '__hlist_for_each_rcu' + - 'hlist_for_each_safe' + - 'hlist_nulls_for_each_entry' + - 'hlist_nulls_for_each_entry_from' + - 'hlist_nulls_for_each_entry_rcu' + - 'hlist_nulls_for_each_entry_safe' + - 'i3c_bus_for_each_i2cdev' + - 'i3c_bus_for_each_i3cdev' + - 'ide_host_for_each_port' + - 'ide_port_for_each_dev' + - 'ide_port_for_each_present_dev' + - 'idr_for_each_entry' + - 'idr_for_each_entry_continue' + - 'idr_for_each_entry_continue_ul' + - 'idr_for_each_entry_ul' + - 'in_dev_for_each_ifa_rcu' + - 'in_dev_for_each_ifa_rtnl' + - 'inet_bind_bucket_for_each' + - 'inet_lhash2_for_each_icsk_rcu' + - 'key_for_each' + - 'key_for_each_safe' + - 'klp_for_each_func' + - 'klp_for_each_func_safe' + - 'klp_for_each_func_static' + - 'klp_for_each_object' + - 'klp_for_each_object_safe' + - 'klp_for_each_object_static' + - 'kunit_suite_for_each_test_case' + - 'kvm_for_each_memslot' + - 'kvm_for_each_vcpu' + - 'list_for_each' + - 'list_for_each_codec' + - 'list_for_each_codec_safe' + - 'list_for_each_continue' + - 'list_for_each_entry' + - 'list_for_each_entry_continue' + - 'list_for_each_entry_continue_rcu' + - 'list_for_each_entry_continue_reverse' + - 'list_for_each_entry_from' + - 'list_for_each_entry_from_rcu' + - 'list_for_each_entry_from_reverse' + - 'list_for_each_entry_lockless' + - 'list_for_each_entry_rcu' + - 'list_for_each_entry_reverse' + - 'list_for_each_entry_safe' + - 'list_for_each_entry_safe_continue' + - 'list_for_each_entry_safe_from' + - 'list_for_each_entry_safe_reverse' + - 'list_for_each_entry_srcu' + - 'list_for_each_prev' + - 'list_for_each_prev_safe' + - 'list_for_each_safe' + - 'llist_for_each' + - 'llist_for_each_entry' + - 'llist_for_each_entry_safe' + - 'llist_for_each_safe' + - 'mci_for_each_dimm' + - 'media_device_for_each_entity' + - 'media_device_for_each_intf' + - 'media_device_for_each_link' + - 'media_device_for_each_pad' + - 'nanddev_io_for_each_page' + - 'netdev_for_each_lower_dev' + - 'netdev_for_each_lower_private' + - 'netdev_for_each_lower_private_rcu' + - 'netdev_for_each_mc_addr' + - 'netdev_for_each_uc_addr' + - 'netdev_for_each_upper_dev_rcu' + - 'netdev_hw_addr_list_for_each' + - 'nft_rule_for_each_expr' + - 'nla_for_each_attr' + - 'nla_for_each_nested' + - 'nlmsg_for_each_attr' + - 'nlmsg_for_each_msg' + - 'nr_neigh_for_each' + - 'nr_neigh_for_each_safe' + - 'nr_node_for_each' + - 'nr_node_for_each_safe' + - 'of_for_each_phandle' + - 'of_property_for_each_string' + - 'of_property_for_each_u32' + - 'pci_bus_for_each_resource' + - 'pcl_for_each_chunk' + - 'pcl_for_each_segment' + - 'pcm_for_each_format' + - 'ping_portaddr_for_each_entry' + - 'plist_for_each' + - 'plist_for_each_continue' + - 'plist_for_each_entry' + - 'plist_for_each_entry_continue' + - 'plist_for_each_entry_safe' + - 'plist_for_each_safe' + - 'pnp_for_each_card' + - 'pnp_for_each_dev' + - 'protocol_for_each_card' + - 'protocol_for_each_dev' + - 'queue_for_each_hw_ctx' + - 'radix_tree_for_each_slot' + - 'radix_tree_for_each_tagged' + - 'rbtree_postorder_for_each_entry_safe' + - 'rdma_for_each_block' + - 'rdma_for_each_port' + - 'rdma_umem_for_each_dma_block' + - 'resource_list_for_each_entry' + - 'resource_list_for_each_entry_safe' + - 'rhl_for_each_entry_rcu' + - 'rhl_for_each_rcu' + - 'rht_for_each' + - 'rht_for_each_entry' + - 'rht_for_each_entry_from' + - 'rht_for_each_entry_rcu' + - 'rht_for_each_entry_rcu_from' + - 'rht_for_each_entry_safe' + - 'rht_for_each_from' + - 'rht_for_each_rcu' + - 'rht_for_each_rcu_from' + - '__rq_for_each_bio' + - 'rq_for_each_bvec' + - 'rq_for_each_segment' + - 'scsi_for_each_prot_sg' + - 'scsi_for_each_sg' + - 'sctp_for_each_hentry' + - 'sctp_skb_for_each' + - 'shdma_for_each_chan' + - '__shost_for_each_device' + - 'shost_for_each_device' + - 'sk_for_each' + - 'sk_for_each_bound' + - 'sk_for_each_entry_offset_rcu' + - 'sk_for_each_from' + - 'sk_for_each_rcu' + - 'sk_for_each_safe' + - 'sk_nulls_for_each' + - 'sk_nulls_for_each_from' + - 'sk_nulls_for_each_rcu' + - 'snd_array_for_each' + - 'snd_pcm_group_for_each_entry' + - 'snd_soc_dapm_widget_for_each_path' + - 'snd_soc_dapm_widget_for_each_path_safe' + - 'snd_soc_dapm_widget_for_each_sink_path' + - 'snd_soc_dapm_widget_for_each_source_path' + - 'tb_property_for_each' + - 'tcf_exts_for_each_action' + - 'udp_portaddr_for_each_entry' + - 'udp_portaddr_for_each_entry_rcu' + - 'usb_hub_for_each_child' + - 'v4l2_device_for_each_subdev' + - 'v4l2_m2m_for_each_dst_buf' + - 'v4l2_m2m_for_each_dst_buf_safe' + - 'v4l2_m2m_for_each_src_buf' + - 'v4l2_m2m_for_each_src_buf_safe' + - 'virtio_device_for_each_vq' + - 'while_for_each_ftrace_op' + - 'xa_for_each' + - 'xa_for_each_marked' + - 'xa_for_each_range' + - 'xa_for_each_start' + - 'xas_for_each' + - 'xas_for_each_conflict' + - 'xas_for_each_marked' + - 'xbc_array_for_each_value' + - 'xbc_for_each_key_value' + - 'xbc_node_for_each_array_value' + - 'xbc_node_for_each_child' + - 'xbc_node_for_each_key_value' + - 'zorro_for_each_dev' + +IncludeBlocks: Preserve # Unknown to clang-format-5.0 +IncludeCategories: + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None # Unknown to clang-format-5.0 +IndentWidth: 8 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto # Unknown to clang-format-5.0 +ObjCBlockIndentWidth: 8 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true + +# Taken from git's rules +PenaltyBreakAssignment: 10 # Unknown to clang-format-4.0 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +PointerAlignment: Right +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false # Unknown to clang-format-4.0 +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCtorInitializerColon: true # Unknown to clang-format-5.0 +SpaceBeforeInheritanceColon: true # Unknown to clang-format-5.0 +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true # Unknown to clang-format-5.0 +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp03 +TabWidth: 8 +UseTab: false \ No newline at end of file diff --git a/Program2/padded/Makefile b/Program2/padded/Makefile new file mode 100644 index 0000000..192b1d7 --- /dev/null +++ b/Program2/padded/Makefile @@ -0,0 +1,7 @@ +all: padded + +padded: padded.c + gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o padded padded.c + +clean: + rm -rf padded diff --git a/Program2/padded/notes.md b/Program2/padded/notes.md new file mode 100644 index 0000000..9807db9 --- /dev/null +++ b/Program2/padded/notes.md @@ -0,0 +1,12 @@ +# CS 491 Lecture 5 + +## Shared Memory + +- Permissions + - 0666 Gives everyone rw access to the memory +- Detatching + - Need to detatch the shared memory or it will persist. +- Tools + - ipcs + - Shows shared memory segments and owners + - t \ No newline at end of file diff --git a/Program2/padded/padded.c b/Program2/padded/padded.c new file mode 100644 index 0000000..5ab5035 --- /dev/null +++ b/Program2/padded/padded.c @@ -0,0 +1,294 @@ +// Ziad Arafat +// Created: Sep 19 2021 +// Figure 1.11 in the textbook + +/** + * @brief This program will create an array of a specified size and fill it + * with random numbers between 1 and 3 inclusive. It will then try to count + * the number of threes in the array using a serial method and a parallel method + * it will return the counts for each attempt as well as the running times. + * + * In this version we will create a struct called padded_int which will contain + * the count as well as a padding of chars set to fill up the chache line of + * the maching we run it on. + */ + +/** +Changelog + +09/18 +- copied code from witharray.c +- created the struct padded_int with the appropriate char padding +- modified the results array to be an array of our padded_int +- modified the thread to correctly access and increment the padded_ints +- + +9/19 +- added free() calls to clean up the memory when done + +09/21 +- fixed the way the array is split for the threads by making the size global + +BUGFIX: The way the array was bieng split was incorrect because unless the array +size was a multiple of the thread count there would be a remaining chunk of +the array. In fact if the thread count is more than half the array size this +chunk could be massive. The fix involved adding an extra loop that gathered the +remaining array elements on the last run. I feel like there is a more elegant +solution but this seems to work well for the purpose of this program? + +*/ + +#define DEBUG 0 // flag to print debug values + +#include +#include +#include +#include + +int *A; // Pointer to our array in heap +int COUNT = 0; // The count of threes we found for the parallel code +int SEGSIZE; // the size of each chunk per tread +int NUMOFTHREADS; // the number of threads to spawn +int SIZE; // size of the array + +// This is a struct that will hold our data in the array for padding. +struct padded_int { + // We need 64B of padding. Because our processor has 64B cache lines + int value; // an int is 4 bytes + char padding[60]; // now we need 64-4 more bytes so we make 60 chars +}; + +struct padded_int *t_results; // an array to store the results from each thread + +pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable + +/** + * @brief Iterates through a chunk of the A array and counts the number of + * threes by updating the COUNT variable + * + * + * @param index The index of the array chunks re are working with + * @return void* returns a void pointer to work with pthread + * + * @pre The global A variable must be a pointer to an array in heap containing numbers between + * 1 and 3. index must be a number referring to the index of the chunk we want + * to work through starting at 0. The global COUNT variable must contain the + * number of threes we have already counted + * + * @post When done the value attribute of the padded int in the array element + * corresponding to the thread must have the correct number of threes we found + * in our chunk of the array + */ +void *count3s(void *idx) +{ + int *index = (int *)idx; // cast the void star input to an int ptr + int mystart = *index * SEGSIZE; // set the starting point for the thread + int myend = mystart + SEGSIZE; // set the end point for the thread + + if (DEBUG) { + printf("I ran once %d\n", *index); + } + + // this temporary variable is here because i am scared of pointers and + // structs so this just makes sure i dont break things. + int valueforthread = 0; + + if (DEBUG) { + printf("start is %d\n", mystart); + printf("end is %d\n", myend); + } + + for (int i = mystart; i < myend; i++) { + if (A[i] == 3) { + valueforthread++; // increment result + } + } + + // When we reach the final index we will calculate the remaining values in the array + // So that we can add them to the result. + // This can become quite high once you go past the 50% mark of the array size + // but our tests will never go that far so this is a pretty good solution. + if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) { + // int remain = SIZE - myend; + // printf("Remainder: %d \n", remain); + // printf("ACTIVE \n"); + for (int i = myend; i < SIZE; i++) { + if (A[i] == 3) { + valueforthread++; + } + } + } + + if (DEBUG) { + printf("value counted: %d \n", valueforthread); + } + + // I am once again scared of pointers so i manually set it to 0 + // and add on the value. + t_results[*index].value = 0; + t_results[*index].value += valueforthread; + + return (void *)0; +} + +/** + * @brief This function is used to spawn the appropriate number of threads and + * also gathers the results of each thread and sums them. + * + * @return int containing the time it took + * + * @pre The global COUNT variable is 0. NUMOFTHREADS is greater than 0. + * SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable + * points to an array in heap. + * + * @post The global COUNT contains the correct three count + * + */ +int count3s_parallel() +{ + // initialize the timer for the parallel work + struct timespec starttime, endtime; + int j, k; + + // get the start time + clock_gettime(CLOCK_REALTIME, &starttime); // start the timer + + // Created an array allocated at runtime to hold the identifiers for + // our threads + pthread_t *t_idents; + int *t_indices; + + // Allocates the space needed for the thread IDs + t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS)); + + // in order to fix the "integer to pointer conversion" warning + // i made an array to store the indices for each thread that way + // i can pass it as a pointer in the for loop + t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS)); + + // We will initialize the padded ints manually in the thread because + // I dont know how calloc will behave with a struct. + t_results = (struct padded_int *)(malloc(sizeof(struct padded_int) * + NUMOFTHREADS)); + + // Create the threads + for (int i = 0; i < NUMOFTHREADS; i++) { + t_indices[i] = i; + pthread_create(&t_idents[i], NULL, count3s, + (void *)&t_indices[i]); + } + COUNT = 0; + // wait for all the treads to finish. + for (int i = 0; i < NUMOFTHREADS; i++) { + pthread_join(t_idents[i], NULL); + COUNT += t_results[i].value; + } + + // free the three arrays we made so we dont leak. + free(t_idents); + free(t_indices); + free(t_results); + + // get the end time + clock_gettime(CLOCK_REALTIME, &endtime); + + // calculate duration + j = endtime.tv_sec - starttime.tv_sec; + k = endtime.tv_nsec - starttime.tv_nsec; + j = j * 1000000000 + k; + + // return the duration + return j; +} + +int main(int argc, char const *argv[]) +{ + // this variable stores the time for generating the random numbers. + time_t t; + + // intitilize the random number with the time as the seed + srand((unsigned)time(&t)); + + if (argc != 3) { + printf("Not correct arguments \n"); + printf("Usage %s \n", + argv[0]); + exit(1); + } + + // Convert the user input to integers + SIZE = atoi(argv[1]); + NUMOFTHREADS = atoi(argv[2]); + + // Set the size of the array chunks to process to the size over the + // number of threads + SEGSIZE = SIZE / NUMOFTHREADS; + + if (DEBUG) { + printf("number is %s \n", argv[1]); + printf("number is %d \n", SIZE); + printf("NumThr is %d \n", NUMOFTHREADS); + printf("Seg Size is %d \n", SEGSIZE); + } + + // Allocate the array based on the input size + A = (int *)(malloc(sizeof(int) * SIZE)); + + // if the array is null barf + if (A == NULL) { + // printf("Austin, we have a problem"); + perror("Austin, we have a problem"); + exit(1); + } + // generate random numbers and fill them in the array + // the numbers are between 1 and 3 inclusive + for (int i = 0; i < SIZE; i++) { + // if we are debugging lets make them all threes. + // that way we can tell if something isnt + // being counted. + if (DEBUG) { + A[i] = 3; + } else { + A[i] = rand() % 4; + } + } + + // Count the threes in parallel + int parallel_time = count3s_parallel(); + + // since the COUNT for the parallel is defined before starting the + // timer i choose to define this one before timer as well. + int local_count = 0; + + // initialize the timer for the parallel work + struct timespec starttime, endtime; + int j, k; + clock_gettime(CLOCK_REALTIME, &starttime); // start the timer + + // Count the threes in serial + for (int i = 0; i < SIZE; i++) { + if (A[i] == 3) { + local_count++; + } + } + + // get end time + clock_gettime(CLOCK_REALTIME, &endtime); + + // calculate duration + j = endtime.tv_sec - starttime.tv_sec; + k = endtime.tv_nsec - starttime.tv_nsec; + j = j * 1000000000 + k; + + // free the array + free(A); + + // print the times and counts + printf("Time parallel: %d \n", parallel_time); + printf("Time serial: %d \n", j); + + printf("parallel count is %d\n", COUNT); + printf("serial count is %d\n", local_count); + + return 0; +} diff --git a/Program2/py_notebooks/Raw Outputs (1).pdf b/Program2/py_notebooks/Raw Outputs (1).pdf new file mode 100644 index 0000000..3ab4716 Binary files /dev/null and b/Program2/py_notebooks/Raw Outputs (1).pdf differ diff --git a/Program2/py_notebooks/Raw Outputs.ipynb b/Program2/py_notebooks/Raw Outputs.ipynb new file mode 100644 index 0000000..48de393 --- /dev/null +++ b/Program2/py_notebooks/Raw Outputs.ipynb @@ -0,0 +1,2354 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "36385637-8b7e-40bf-b717-3649ae71e7f3", + "metadata": {}, + "source": [ + "# Raw data and graphs\n", + "This pdf containts the raw data and graphs used to test the performance of the code.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "2ff4494e-1b45-4168-b69b-839aec2c538f", + "metadata": {}, + "source": [ + "## Setup 1\n", + "- Threads all increment one variable **without a mutex**\n", + "\n", + "##### code\n", + "\n", + "```c\n", + "// Ziad Arafat\n", + "// Created: Aug 31 2021\n", + "// Figure 1.7 in the textbook\n", + "\n", + "/**\n", + " * @brief This program will create an array of a specified size and fill it\n", + " * with random numbers between 1 and 3 inclusive. It will then try to count\n", + " * the number of threes in the array using a serial method and a parallel method\n", + " * it will return the counts for each attempt as well as the running times. \n", + " * \n", + " * This version will use a single variable shared by all the threads without\n", + " * uring a mutex. We expect it to return incorrect results\n", + " * \n", + " */\n", + "\n", + "/**\n", + "Changelog\n", + "08/31\n", + "- added argument handling\n", + "- Added handling to create an array of a certain size.\n", + "- added random number generator to fill array\n", + "- Created serial program to count 3s in array \n", + "\n", + "09/07\n", + "- tested thread creation using pthread\n", + "- created function to generate threads to handle different sections of array\n", + "\n", + "09/10\n", + "- corrected my thread code because it wasnt being joined so output was shown\n", + "- before thread was closed.\n", + "\n", + "09/11\n", + "- added timing code.\n", + "\n", + "09/12\n", + "- fixed timing code. \n", + "\n", + "09/19\n", + "- added free() calls to clean up the memory when done\n", + "\n", + "09/21\n", + "- fixed the way the array is split for the threads\n", + "\n", + "*/\n", + "\n", + "#define DEBUG 0 // flag to print debug values\n", + "\n", + "#include \n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "int *A; // Pointer to our array in heap\n", + "int COUNT = 0; // The count of threes we found for the parallel code\n", + "int SEGSIZE; // the size of each chunk per tread\n", + "int NUMOFTHREADS; // the number of threads to spawn\n", + "int SIZE; // The size of the input array\n", + "\n", + "/**\n", + " * @brief Iterates through a chunk of the A array and counts the number of \n", + " * threes by updating the COUNT variable\n", + " * \n", + " * \n", + " * @param index The index of the array chunks re are working with\n", + " * @return void* returns a void pointer to work with pthread\n", + " * \n", + " * @pre The global A variable must be a pointer to an array in heap containing numbers between \n", + " * 1 and 3. index must be a number referring to the index of the chunk we want\n", + " * to work through starting at 0. The global COUNT variable must contain the\n", + " * number of threes we have already counted\n", + " * \n", + " * @post When done the COUNT variable must be updated to have the number of \n", + " * threes found in our chunk of the array\n", + " */\n", + "void *count3s(void *idx)\n", + "{\n", + " // if (DEBUG)\n", + " // printf(\"I ran once %d\\n\", index);\n", + "\n", + " int *index = (int *)idx; // cast the void star input to an int ptr\n", + " int mystart = *index * SEGSIZE; // set the starting point for the thread\n", + " int myend = mystart + SEGSIZE; // set the end point for the thread\n", + "\n", + " if (DEBUG) {\n", + " printf(\"start is %d\\n\", mystart);\n", + " printf(\"end is %d\\n\", myend);\n", + " }\n", + "\n", + " // iterate through our section of the array and count the threes\n", + " for (int i = mystart; i < myend; i++) {\n", + " if (A[i] == 3) {\n", + " if (DEBUG)\n", + " printf(\"Found a three\\n\");\n", + " COUNT++;\n", + " }\n", + " }\n", + "\n", + " // When we reach the final index we will calculate the remaining values in the array\n", + " // So that we can add them to the result.\n", + " // This can become quite high once you go past the 50% mark of the array size\n", + " // but our tests will never go that far so this is a pretty good solution.\n", + " if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) {\n", + " if (DEBUG) {\n", + " int remain = SIZE - myend;\n", + " printf(\"Remainder: %d \\n\", remain);\n", + " }\n", + "\n", + " // printf(\"ACTIVE \\n\");\n", + "\n", + " // iterate through the remainder of the array and count the 3s\n", + " for (int i = myend; i < SIZE; i++) {\n", + " if (A[i] == 3) {\n", + " COUNT++; // increment count\n", + " }\n", + " }\n", + "\n", + " if (DEBUG) {\n", + " printf(\"The count is now %d threads %d index %d SIZE %d end %d \\n\",\n", + " COUNT, NUMOFTHREADS, *index, SIZE, myend);\n", + " }\n", + " }\n", + "\n", + " return (void *)0;\n", + "}\n", + "\n", + "/**\n", + " * @brief This function is used to spawn the appropriate number of threads as \n", + " * well as keep track of their progress. \n", + " * \n", + " * @return int containing the time it took\n", + " * \n", + " * @pre The global COUNT variable is 0. NUMOFTHREADS is greater than 0.\n", + " * SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable\n", + " * points to an array in heap. \n", + " * \n", + " * @post The global COUNT contains the correct three count\n", + " * \n", + " */\n", + "int count3s_parallel()\n", + "{\n", + " // initialize the timer for the parallel work\n", + " struct timespec starttime, endtime;\n", + " int j, k;\n", + "\n", + " // get the start time\n", + " clock_gettime(CLOCK_REALTIME, &starttime); // start the timer\n", + "\n", + " // Created an array allocated at runtime to hold the identifiers for\n", + " // our threads\n", + " pthread_t *t_idents;\n", + " int *t_indices;\n", + "\n", + " // Allocates the space needed for the thread IDs\n", + " t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS));\n", + "\n", + " // in order to fix the \"integer to pointer conversion\" warning\n", + " // i made an array to store the indices for each thread that way\n", + " // i can pass it as a pointer in the for loop\n", + " t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS));\n", + "\n", + " // Create the threads\n", + " for (int i = 0; i < NUMOFTHREADS; i++) {\n", + " t_indices[i] = i;\n", + " pthread_create(&t_idents[i], NULL, count3s,\n", + " (void *)&t_indices[i]);\n", + " }\n", + "\n", + " // wait for all the treads to finish.\n", + " for (int i = 0; i < NUMOFTHREADS; i++) {\n", + " pthread_join(t_idents[i], NULL);\n", + " }\n", + "\n", + " // free the ids and indexes in memory so we dont leak\n", + " free(t_idents);\n", + " free(t_indices);\n", + "\n", + " // get the end time\n", + " clock_gettime(CLOCK_REALTIME, &endtime);\n", + "\n", + " // calculate duration\n", + " j = endtime.tv_sec - starttime.tv_sec;\n", + " k = endtime.tv_nsec - starttime.tv_nsec;\n", + " j = j * 1000000000 + k;\n", + "\n", + " // return the duration\n", + " return j;\n", + "}\n", + "\n", + "int main(int argc, char const *argv[])\n", + "{\n", + " // this variable stores the time for generating the random numbers.\n", + " time_t t;\n", + "\n", + " // intitilize the random number with the time as the seed\n", + " srand((unsigned)time(&t));\n", + "\n", + " // check if the correct arguments are used\n", + " if (argc != 3) {\n", + " // if they are wrong barf and exit\n", + " printf(\"Not correct arguments \\n\");\n", + " printf(\"Usage %s \\n\",\n", + " argv[0]);\n", + " exit(1);\n", + " }\n", + "\n", + " // Convert the user input to integers\n", + " SIZE = atoi(argv[1]);\n", + " NUMOFTHREADS = atoi(argv[2]);\n", + "\n", + " // Set the size of the array chunks to process to the size over the\n", + " // number of threads\n", + " SEGSIZE = SIZE / NUMOFTHREADS;\n", + "\n", + " if (DEBUG) {\n", + " printf(\"number is %s \\n\", argv[1]);\n", + " printf(\"number is %d \\n\", SIZE);\n", + " printf(\"NumThr is %d \\n\", NUMOFTHREADS);\n", + " printf(\"Seg Size is %d \\n\", SEGSIZE);\n", + " }\n", + "\n", + " // Allocate the array based on the input size\n", + " A = (int *)(malloc(sizeof(int) * SIZE));\n", + "\n", + " // if the array is null barf\n", + " if (A == NULL) {\n", + " printf(\"Austin, we have a problem\");\n", + " exit(1);\n", + " }\n", + "\n", + " // generate random numbers and fill them in the array\n", + " // the numbers are between 1 and 3 inclusive\n", + " for (int i = 0; i < SIZE; i++) {\n", + " A[i] = rand() % 4;\n", + " // printf(\"E%d \\n\", A[i]);\n", + " }\n", + "\n", + " // Count the threes in parallel\n", + " int parallel_time = count3s_parallel();\n", + "\n", + " // initialize the count for the serial code\n", + " int local_count = 0;\n", + "\n", + " // initialize the timer for the parallel work\n", + " struct timespec starttime, endtime;\n", + " int j, k;\n", + "\n", + " clock_gettime(CLOCK_REALTIME, &starttime); // start the timer\n", + "\n", + " // Count the threes in serial\n", + " for (int i = 0; i < SIZE; i++) {\n", + " if (A[i] == 3) {\n", + " local_count++;\n", + " }\n", + " }\n", + "\n", + " // get end time\n", + " clock_gettime(CLOCK_REALTIME, &endtime);\n", + "\n", + " // calculate duration\n", + " j = endtime.tv_sec - starttime.tv_sec;\n", + " k = endtime.tv_nsec - starttime.tv_nsec;\n", + " j = j * 1000000000 + k;\n", + "\n", + " // free the array\n", + " free(A);\n", + "\n", + " // print the times and counts\n", + " printf(\"Time parallel: %d \\n\", parallel_time);\n", + " printf(\"Time serial: %d \\n\", j);\n", + "\n", + " printf(\"parallel count is %d\\n\", COUNT);\n", + " printf(\"serial count is %d\\n\", local_count);\n", + "\n", + " return 0;\n", + "}\n", + "\n", + "\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "09257718-40a4-42e6-9aeb-6f087c84add6", + "metadata": { + "jp-MarkdownHeadingCollapsed": true, + "tags": [] + }, + "source": [ + "#### Raw console outputs\n", + "\n", + "- All runs are done with an array of size $10,000,000$\n", + "\n", + "##### 1 Thread\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/parallel-programming (main)> cd figure1_7/\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> make\n", + "gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -o figure1_7 figure1_7.c\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 1\n", + "Time parallel: 41548092 \n", + "Time serial: 40781794 \n", + "parallel count is 2499294\n", + "serial count is 2499294\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 1\n", + "Time parallel: 41616537 \n", + "Time serial: 40523451 \n", + "parallel count is 2499311\n", + "serial count is 2499311\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 1\n", + "Time parallel: 41686727 \n", + "Time serial: 40876149 \n", + "parallel count is 2498476\n", + "serial count is 2498476\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 1\n", + "Time parallel: 40781165 \n", + "Time serial: 40663692 \n", + "parallel count is 2501086\n", + "serial count is 2501086\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 1\n", + "Time parallel: 42017216 \n", + "Time serial: 40238289 \n", + "parallel count is 2499818\n", + "serial count is 2499818\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> \n", + "\n", + "```\n", + "\n", + "##### 2 Threads\n", + "\n", + "- Since the output was not the same for parallel likely due to the race condition caused by line 85 we will stop testing this code\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 2\n", + "Time parallel: 62501107 \n", + "Time serial: 39678580 \n", + "parallel count is 2186343\n", + "serial count is 2499866\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 2\n", + "Time parallel: 61941468 \n", + "Time serial: 40740028 \n", + "parallel count is 2185462\n", + "serial count is 2498949\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 2\n", + "Time parallel: 41536079 \n", + "Time serial: 41486353 \n", + "parallel count is 2300733\n", + "serial count is 2499709\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 2\n", + "Time parallel: 62254078 \n", + "Time serial: 40387819 \n", + "parallel count is 2180401\n", + "serial count is 2499807\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> ./figure1_7 10000000 2\n", + "Time parallel: 62037290 \n", + "Time serial: 40656079 \n", + "parallel count is 2184139\n", + "serial count is 2499412\n", + "doge@godemperordoge1998 ~/D/p/figure1_7 (main)> \n", + "```\n" + ] + }, + { + "cell_type": "markdown", + "id": "bec6f9a3-0ddb-4cc9-83b3-ee97238d0735", + "metadata": {}, + "source": [ + "## Setup 2\n", + "- Threads all increment one variable **with a mutex**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "1a51eb83-8591-463d-a3d5-f6dab298c9f5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Text(0, 0.5, 'time in 10million ms')" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEZCAYAAACdNT5ZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAa3UlEQVR4nO3de5RU5Z3u8e+j4CDiFTiKIjbGERFUjO3gHBgX4g0NxhmXikSSeBmZqKAu0agjmRCz5oyi44mZqBFjAhp1YjjRKDHmNuMY71wEaYPmoqjtFUERROTi7/xRu6Fpu6s3XbWrqnc/n7VqNbX3rnf/qhY8vP3Wu9+tiMDMzPJnm2oXYGZm2XDAm5nllAPezCynHPBmZjnlgDczyykHvJlZTjngzcxyygFvuSJppKQnJa2UtELSE5IOT/nakLRfRnXtL+nnkpYldf1K0qAszmXWxAFvuSFpJ2AO8B/AbsBewLeAT6pZV2IX4EFgELA78Czw82oWZPknX8lqeSGpHvhtROxS5JhzgMuBPSiE7MSIeFXSY8DfAWuAAM4Ftgf+MSJGNnt9AH8dEX+WNBNYC3wOOAJYAHwlIl5NUetuwHKgT0Qs78DbNWuXe/CWJ38ENkqaJekESbs23ynpZOCfgVOAvsDvgXsBIuLI5LBDIqJXRPwk5TnPBL4N9AEWAnenfN2RwNsOd8tSzQW8pB9KeldSQ4pjB0j6b0nPSXpe0omVqNFqU0R8CIyk0AO/HVgm6UFJuyeHfA34t4hYEhEbgP8DDJO0Twmn/UVEPBYRnwBXA38rae9iL5DUH7gZuLSE85q1q+YCHpgJjEl57FTgvog4FDgDuCWroqxzSML7rIjoDwwF9gS+k+zeB7hJ0geSPgBWAKIwVt9Rrzc79+qkzT3bOlhSX+DXwC0RcW8J5zVrV80FfEQ8RuEfySaSPifpEUnzJf1e0gFNhwM7JX/eGXizgqVajYuIFyl0GIYmm14H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maZGkRRTWyG+VpCskLU6OvTbZNkzS05Kel3S/pF2T7Y8mS8wiqY+kpcmfz5L0M0mPSPqTpOnJ9muB7ZP3e3f5P07rChzw1llcCfwlIoZFxOXJtkOBS4ADgX2BEc2OX57cpOS3wFTgmOT5PODS5JjvRcThETEU2B4Ym2z/ETA5Ig5pqxhJJwAnA8OT46Ynu+4EroiIg4HFwDdTvLdhwDgKq3aOk7R3siztx8n7PTNFG2af4YC3zuzZiGhMlvhdCNQ12/eT5OcRFP4DeELSQuCrwD7JvqMkPSNpMYUVAYckqyXuEhGPJcfc1ca5j6GwEukagIhYIWnn5LVNa8fPorDqYHt+FxErI2IthZuN7NPeC8zS8IVO1pl90uzPG9ny7/NHyU8Bv4mI8c1fKKkHcAtQHxGvS5oG9Miw1g1s7lC1PE+x92HWYe7BW2exCtixA697GhghaT8ASTskd+BpCtn3kjH5UwEi4gPgA0kjk/1tDY/8BjhbUs+k3d0iYiXwvqS/S475MtDUm18KHJb8+dSUta/3ja2tFA546xSSGxs/kXwhen27L9j8umXAWcC9yWJRTwEHJEF+O9AA/AqY2+xlZwM3J0M6rd6uMCIeAR4E5iXHXZbs+ipwfXKuYcA1yfYbgPMlPQf0SVn+DOB5f8lqHeWlCszMcso9eDOznHLAm5nllAPezCynHPBmZjnlgDczyykHvJlZTjngzcxyygFvZpZT/x8EpMe13FoOrAAAAABJRU5ErkJggg==\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "\n", + "import matplotlib.pyplot as plt\n", + "import pandas\n", + "ser = [84013074, 81465166.2, 85934827.4, 80388688.8, 80466492]\n", + "par = [120240643.2, 308139976, 629666501.2, 712601966.2, 651475517.6]\n", + "\n", + "val_dict = {}\n", + "\n", + "for k, v in enumerate(par):\n", + " val_dict[str(2**k)]={\n", + " \"serial\": ser[k],\n", + " \"parall\": par[k]\n", + " }\n", + "\n", + "df1 = pandas.DataFrame.from_dict(val_dict, orient='index')\n", + "\n", + "\n", + "\n", + "df1.plot.bar(title='Setup 2')\n", + "plt.xlabel(\"thread count\")\n", + "plt.ylabel(\"time in 10million ms\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "4da815ab-e7e4-4c5f-aae5-f52a3606a253", + "metadata": {}, + "source": [ + "\n", + "\n", + "##### code\n", + "```c\n", + "// Ziad Arafat\n", + "// Created: Sep 17 2021\n", + "// Figure 1.9 in the textbook\n", + "\n", + "/**\n", + " * @brief This program will create an array of a specified size and fill it\n", + " * with random numbers between 1 and 3 inclusive. It will then try to count\n", + " * the number of threes in the array using a serial method and a parallel method\n", + " * it will return the counts for each attempt as well as the running times. \n", + " * \n", + " * \n", + " * This version adds a mutex to prevent the race condition. We expect the \n", + " * the outputs to be correct but the performance will be very bad because\n", + " * the threads will all be waiting on each other and locking/unlocking the\n", + " * mutex causing an overhead with the OS.\n", + " */\n", + "\n", + "/**\n", + "Changelog\n", + "\n", + "09/17\n", + "- Copied code from the figure1_7.c\n", + "- added mutux initializer\n", + "- added lock and unlock to the thread function\n", + "\n", + "\n", + "09/19\n", + "- added free() calls to clean up the memory when done\n", + "\n", + "09/21\n", + "- fixed the way the array is split for the threads\n", + "\n", + "\n", + "*/\n", + "\n", + "#define DEBUG 0 // flag to print debug values\n", + "\n", + "#include \n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "int *A; // Pointer to our array in heap\n", + "int COUNT = 0; // The count of threes we found for the parallel code\n", + "int SEGSIZE; // the size of each chunk per tread\n", + "int NUMOFTHREADS; // the number of threads to spawn\n", + "int SIZE; // The size of the input array\n", + "\n", + "pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable\n", + "\n", + "/**\n", + " * @brief Iterates through a chunk of the A array and counts the number of \n", + " * threes by updating the COUNT variable\n", + " * \n", + " * \n", + " * @param index The index of the array chunks re are working with\n", + " * @return void* returns a void pointer to work with pthread\n", + " * \n", + " * @pre The global A variable must be a pointer to an array in heap containing numbers between \n", + " * 1 and 3. index must be a number referring to the index of the chunk we want\n", + " * to work through starting at 0. The global COUNT variable must contain the\n", + " * number of threes we have already counted\n", + " * \n", + " * @post When done the COUNT variable must be updated to have the number of \n", + " * threes found in our chunk of the array\n", + " */\n", + "void *count3s(void *idx)\n", + "{\n", + " // if (DEBUG)\n", + " // printf(\"I ran once %d\\n\", index);\n", + "\n", + " int *index = (int *)idx; // cast the void star input to an int ptr\n", + " int mystart = *index * SEGSIZE; // set the starting point for the thread\n", + " int myend = mystart + SEGSIZE; // set the end point for the thread\n", + "\n", + " if (DEBUG) {\n", + " printf(\"start is %d\\n\", mystart);\n", + " printf(\"end is %d\\n\", myend);\n", + " }\n", + "\n", + " // iterate through our section of the array and count the threes\n", + " for (int i = mystart; i < myend; i++) {\n", + " if (A[i] == 3) {\n", + " if (DEBUG) {\n", + " printf(\"Found a three\\n\");\n", + " }\n", + "\n", + " // lock the mutex thereby waiting until other threads\n", + " // are done with this section of the code before\n", + " // locking it.\n", + " pthread_mutex_lock(&mtex); // lock the mutex\n", + "\n", + " COUNT++; // increment count\n", + "\n", + " // unlock the mutex to let the OS know we are done\n", + " // with this section of code.\n", + " pthread_mutex_unlock(&mtex);\n", + " }\n", + " }\n", + "\n", + " // When we reach the final index we will calculate the remaining values in the array\n", + " // So that we can add them to the result.\n", + " // This can become quite high once you go past the 50% mark of the array size\n", + " // but our tests will never go that far so this is a pretty good solution.\n", + " if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) {\n", + " if (DEBUG) {\n", + " int remain = SIZE - myend;\n", + " printf(\"Remainder: %d \\n\", remain);\n", + " }\n", + "\n", + " // iterate through the remainder of the array and count the 3s\n", + " for (int i = myend; i < SIZE; i++) {\n", + " // if its a three in the array\n", + " if (A[i] == 3) {\n", + " // lock the mutex thereby waiting until other threads\n", + " // are done with this section of the code before\n", + " // locking it.\n", + " pthread_mutex_lock(&mtex); // lock the mutex\n", + "\n", + " COUNT++; // increment count\n", + "\n", + " // unlock the mutex to let the OS know we are done\n", + " // with this section of code.\n", + " pthread_mutex_unlock(&mtex);\n", + " }\n", + " }\n", + "\n", + " if (DEBUG) {\n", + " printf(\"The count is now %d threads %d index %d SIZE %d end %d \\n\",\n", + " COUNT, NUMOFTHREADS, *index, SIZE, myend);\n", + " }\n", + " }\n", + "\n", + " return (void *)0;\n", + "}\n", + "\n", + "/**\n", + " * @brief This function is used to spawn the appropriate number of threads\n", + " * \n", + " * @return int containing the time it took\n", + " * \n", + " * @pre The global COUNT variable is 0. NUMOFTHREADS is greater than 0.\n", + " * SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable\n", + " * points to an array in heap. \n", + " * \n", + " * @post The global COUNT contains the correct three count\n", + " * \n", + " */\n", + "int count3s_parallel()\n", + "{\n", + " // initialize the timer for the parallel work\n", + " struct timespec starttime, endtime;\n", + " int j, k;\n", + "\n", + " // get the start time\n", + " clock_gettime(CLOCK_REALTIME, &starttime); // start the timer\n", + "\n", + " // Created an array allocated at runtime to hold the identifiers for\n", + " // our threads\n", + " pthread_t *t_idents;\n", + " int *t_indices;\n", + "\n", + " // Allocates the space needed for the thread IDs\n", + " t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS));\n", + "\n", + " // in order to fix the \"integer to pointer conversion\" warning\n", + " // i made an array to store the indices for each thread that way\n", + " // i can pass it as a pointer in the for loop\n", + " t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS));\n", + "\n", + " // Create the threads\n", + " for (int i = 0; i < NUMOFTHREADS; i++) {\n", + " t_indices[i] = i;\n", + " pthread_create(&t_idents[i], NULL, count3s,\n", + " (void *)&t_indices[i]);\n", + " }\n", + "\n", + " // wait for all the treads to finish.\n", + " for (int i = 0; i < NUMOFTHREADS; i++) {\n", + " pthread_join(t_idents[i], NULL);\n", + " }\n", + "\n", + " // free the ids and indexes in memory so we dont leak\n", + " free(t_idents);\n", + " free(t_indices);\n", + "\n", + " // get the end time\n", + " clock_gettime(CLOCK_REALTIME, &endtime);\n", + "\n", + " // calculate duration\n", + " j = endtime.tv_sec - starttime.tv_sec;\n", + " k = endtime.tv_nsec - starttime.tv_nsec;\n", + " j = j * 1000000000 + k;\n", + "\n", + " // return the duration\n", + " return j;\n", + "}\n", + "\n", + "int main(int argc, char const *argv[])\n", + "{\n", + " // this variable stores the time for generating the random numbers.\n", + " time_t t;\n", + "\n", + " // intitilize the random number with the time as the seed\n", + " srand((unsigned)time(&t));\n", + "\n", + "\n", + " // check the user arguments\n", + " if (argc != 3) {\n", + " // if they are wrong count barf\n", + " printf(\"Not correct arguments \\n\");\n", + " printf(\"Usage %s r of threads> \\n\",\n", + " argv[0]);\n", + " exit(1);\n", + " }\n", + "\n", + " // Convert the user input to integers\n", + " SIZE = atoi(argv[1]);\n", + " NUMOFTHREADS = atoi(argv[2]);\n", + "\n", + " // Set the size of the array chunks to process to the size over the\n", + " // number of threads\n", + " SEGSIZE = SIZE / NUMOFTHREADS;\n", + "\n", + " if (DEBUG) {\n", + " printf(\"number is %s \\n\", argv[1]);\n", + " printf(\"number is %d \\n\", SIZE);\n", + " printf(\"NumThr is %d \\n\", NUMOFTHREADS);\n", + " printf(\"Seg Size is %d \\n\", SEGSIZE);\n", + " }\n", + "\n", + " // Allocate the array based on the input size\n", + " A = (int *)(malloc(sizeof(int) * SIZE));\n", + "\n", + " // if the array is null barf\n", + " if (A == NULL) {\n", + " perror(\"Austin, we have a problem\");\n", + " // printf(\"Austin, we have a problem\");\n", + " exit(1);\n", + " }\n", + "\n", + " // generate random numbers and fill them in the array\n", + " // the numbers are between 1 and 3 inclusive\n", + " for (int i = 0; i < SIZE; i++) {\n", + " A[i] = rand() % 4;\n", + "\n", + " // printf(\"E%d \\n\", A[i]);\n", + " }\n", + "\n", + " // Count the threes in parallel\n", + " int parallel_time = count3s_parallel();\n", + "\n", + " // since the COUNT for the parallel is defined before starting the\n", + " // timer i choose to define this one before timer as well.\n", + " int local_count = 0;\n", + "\n", + " // initialize the timer for the parallel work\n", + " struct timespec starttime, endtime;\n", + " int j, k;\n", + " clock_gettime(CLOCK_REALTIME, &starttime); // start the timer\n", + "\n", + " // Count the threes in serial\n", + " for (int i = 0; i < SIZE; i++) {\n", + " if (A[i] == 3) {\n", + " local_count++;\n", + " }\n", + " }\n", + "\n", + " // get end time\n", + " clock_gettime(CLOCK_REALTIME, &endtime);\n", + "\n", + " // calculate duration\n", + " j = endtime.tv_sec - starttime.tv_sec;\n", + " k = endtime.tv_nsec - starttime.tv_nsec;\n", + " j = j * 1000000000 + k;\n", + "\n", + " // free the array\n", + " free(A);\n", + "\n", + " // print the times and counts\n", + " printf(\"Time parallel: %d \\n\", parallel_time);\n", + " printf(\"Time serial: %d \\n\", j);\n", + "\n", + " printf(\"parallel count is %d\\n\", COUNT);\n", + " printf(\"serial count is %d\\n\", local_count);\n", + "\n", + " return 0;\n", + "}\n", + "\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "6f4d9d42-22b2-4423-9231-34439ebe11f0", + "metadata": {}, + "source": [ + "#### Raw console outputs\n", + "##### 1 Thread\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> make\n", + "gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o withmutex withmutex.c\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 1\n", + "Time parallel: 120021411 \n", + "Time serial: 78854234 \n", + "parallel count is 2501472\n", + "serial count is 2501472\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 1\n", + "Time parallel: 120497170 \n", + "Time serial: 81333878 \n", + "parallel count is 2499989\n", + "serial count is 2499989\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 1\n", + "Time parallel: 120347221 \n", + "Time serial: 87031321 \n", + "parallel count is 2501315\n", + "serial count is 2501315\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 1\n", + "Time parallel: 120116256 \n", + "Time serial: 78876653 \n", + "parallel count is 2501360\n", + "serial count is 2501360\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 1\n", + "Time parallel: 120221158 \n", + "Time serial: 93969284 \n", + "parallel count is 2501360\n", + "serial count is 2501360\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> \n", + "```\n", + "\n", + "##### 2 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 2\n", + "Time parallel: 278855092 \n", + "Time serial: 81221853 \n", + "parallel count is 2499954\n", + "serial count is 2499954\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 2\n", + "Time parallel: 315544116 \n", + "Time serial: 89160223 \n", + "parallel count is 2501195\n", + "serial count is 2501195\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 2\n", + "Time parallel: 309496769 \n", + "Time serial: 79645116 \n", + "parallel count is 2499936\n", + "serial count is 2499936\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 2\n", + "Time parallel: 310133511 \n", + "Time serial: 78546513 \n", + "parallel count is 2499456\n", + "serial count is 2499456\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 2\n", + "Time parallel: 326670392 \n", + "Time serial: 78752126 \n", + "parallel count is 2500418\n", + "serial count is 2500418\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> \n", + "\n", + "```\n", + "\n", + "##### 4 threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 4\n", + "Time parallel: 621104281 \n", + "Time serial: 78621872 \n", + "parallel count is 2500078\n", + "serial count is 2500078\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 4\n", + "Time parallel: 628150499 \n", + "Time serial: 92534186 \n", + "parallel count is 2499876\n", + "serial count is 2499876\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 4\n", + "Time parallel: 612589789 \n", + "Time serial: 88590668 \n", + "parallel count is 2498990\n", + "serial count is 2498990\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 4\n", + "Time parallel: 631838048 \n", + "Time serial: 78806044 \n", + "parallel count is 2501489\n", + "serial count is 2501489\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 4\n", + "Time parallel: 654649889 \n", + "Time serial: 91121367 \n", + "parallel count is 2499659\n", + "serial count is 2499659\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> \n", + "```\n", + "\n", + "##### 8 threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 8\n", + "Time parallel: 735814192 \n", + "Time serial: 78948171 \n", + "parallel count is 2500200\n", + "serial count is 2500200\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 8\n", + "Time parallel: 637441065 \n", + "Time serial: 83372267 \n", + "parallel count is 2500024\n", + "serial count is 2500024\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 8\n", + "Time parallel: 733727894 \n", + "Time serial: 78821827 \n", + "parallel count is 2502801\n", + "serial count is 2502801\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 8\n", + "Time parallel: 715607851 \n", + "Time serial: 79105103 \n", + "parallel count is 2501713\n", + "serial count is 2501713\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 8\n", + "Time parallel: 740418829 \n", + "Time serial: 81696076 \n", + "parallel count is 2500349\n", + "serial count is 2500349\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> \n", + "\n", + "```\n", + "\n", + "##### 16 threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 16\n", + "Time parallel: 670948609 \n", + "Time serial: 78550634 \n", + "parallel count is 2497839\n", + "serial count is 2497839\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 16\n", + "Time parallel: 669674494 \n", + "Time serial: 78881891 \n", + "parallel count is 2500331\n", + "serial count is 2500331\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 16\n", + "Time parallel: 606297090 \n", + "Time serial: 82304114 \n", + "parallel count is 2501547\n", + "serial count is 2501547\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 16\n", + "Time parallel: 677565931 \n", + "Time serial: 83745498 \n", + "parallel count is 2499270\n", + "serial count is 2499270\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> ./withmutex 10000000 16\n", + "Time parallel: 632891464 \n", + "Time serial: 78850323 \n", + "parallel count is 2502719\n", + "serial count is 2502719\n", + "doge@godemperordoge1998 ~/D/p/withmutex (main)> \n", + "```\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "68b3db4f-0b7c-42af-8b82-13b852538687", + "metadata": {}, + "source": [ + "## Setup 3\n", + "- Each thread increments it's own count variable in an array **without padding**" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "dded1e57-61be-4980-9238-325776773fed", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Text(0, 0.5, 'time in 10million ms')" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEfCAYAAABMAsEUAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAhrklEQVR4nO3deZhU1bnv8e9PwSDiDFEUFIwTgopJK+bqSXAMGqPnGieUGIdIHMAY0ahHEom5J8fhxFxzxAGNwXgdYjjGgwY1o5pBDI0yNCIJUaKtRhEQQUQG3/vH3i1l20PR9N5V3fv3eZ5+umrvVXu9DdX91hr2WooIzMysuDaqdABm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XXnopzzzzDAsXLvwwEaxevZo5c+a0ep1ly5bRu3dvVq9ezT333JNZvG4RFF1rs5c8c8k6gSynezZl9uzZXHrppWy00UZ07dqVW265hS5dunDhhReydOlS1qxZw0UXXcTAgQNbvM73vvc9hgwZQq9evRgyZAjLli3LJN4Ot2dxTU1NdIqNaapl+qgTgXVCc+fOZcCAAZUOo2Ka+vklTY+ImqbKu2vIzKzgnAjMzArOicDMrOCcCMzMCs6JwMys4JwIzMwKzvcRmFnnV8507fW6XmWmVU+cOJHa2lpuuummdl2K2i0CM7MqsmbNmtzrdCIwM2tnCxYsYM899+S0005jwIABnHDCCaxYsYKrr76a/fffn0GDBjFy5EgabugdOnQoF110ETU1Ndx44408/PDDDBkyhP3224/DDz+cN954I9N4nQjMzDIwb948zj//fObOncsWW2zBzTffzKhRo5g2bRp1dXW89957PPLIIx+WL12K+uCDD2bq1Kk899xznHLKKVx33XWZxuoxAjOzDPTt25eDDjoIgBEjRvCjH/2I/v37c91117FixQoWL17MwIED+dKXvgR8dCnq+vp6Tj75ZF5//XVWrVpF//79M43VLQIzswxI+tjz888/n0mTJjF79mzOOeccVq5c+eH50qWoR48ezahRo5g9eza33XbbR8plwYnAzCwDL7/88ofLTt97770cfPDBAPTs2ZPly5czadKkZl+7dOlSdtxxRwDuuuuuzGN115CZdX4VmO65xx57MH78eM466yz22msvzjvvPJYsWcKgQYPYfvvt2X///Zt97bhx4zjxxBPZeuutOfTQQ3nppZcyjdXLUFeKl6E2y0yll6FesGABxxxzDHV1dRWp38tQm5nZenEiMDNrZ/369atYa6AtnAjMrFPqaN3e7aUtP7cTgZl1Ot26dWPRokWFSwYRwaJFi+jWrdt6vc6zhsys0+nTpw/19fUsXLiw0qHkrlu3bvTp02e9XuNEYGadTteuXTO/G7czyaxrSNKdkt6U1OSIiRI/kjRf0ixJn84qFjMza16WYwQTgWEtnD8K2C39GgnckmEsZmbWjMwSQUQ8BSxuochxwE8jMRXYSlLvrOIxM7OmVXLW0I7AKyXP69NjZ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MbJIN41szGDiZZD77yZL6RsTlwHvpz3taGdcw+xgnAiuCv0REfbqy5QygX8m5n6XfDyRJFH+SNINkGeid03OHSHomXX7gUGCgpK2ArSLiqbTM3c3UfTjwk3TPASJicbrfwFYR8WRa5i7gc2X8HL+NiKURsRJ4viQ+sw3iG8qsCEqXe17LR9/3DXvhCvh1RAwvfaGkbsDNJJuUvCJpHNAtw1jXsO4DWuN6Wvo5zNrMLQLrbJaxbj359TEVOEjSrgCSNku3amz4Y/xWOmZwAkBEvA28nS4RDNBct8yvgTMldU+vu026EcwSSf+SlvkKyY5UAAtYt1vZCWXGvlpS1zLLmn2ME4F1KhGxiKR7p65ksLic1y0EzgDuSxdAexrYM/2DfzvJ5uWPk2wj2OBMYHzaldTkGjDpMsqTgdq03CXpqa8C16d1DQauTo//J3CepOdINkUpxwRglgeLra28xISZWcG5RWBmVnBOBGZmBedEYGZWcE4EZmYF50RgZlZwTgRmZgXnRGBmVnD/HyGV1isufqFVAAAAAElFTkSuQmCC\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "ser = [77342337.6, 76163542.4, 75757667, 76600539.4, 76741758.4, 76538017.4, 76620890.8, 77830947.6, 76217962.6, 76562336.2]\n", + "par = [80784200.2, 118902330.6, 115032536.8, 75653505.8, 51827737.4, 41478460.2, 26468548.4, 25685669.4, 31348666.2, 46536505.8]\n", + "\n", + "val_dict = {}\n", + "\n", + "for k, v in enumerate(par):\n", + " val_dict[str(2**k)]={\n", + " \"serial\": ser[k],\n", + " \"parall\": par[k]\n", + " }\n", + "\n", + "df1 = pandas.DataFrame.from_dict(val_dict, orient='index')\n", + "\n", + "\n", + "\n", + "df1.plot.bar(title='Setup 3')\n", + "plt.xlabel(\"thread count\")\n", + "plt.ylabel(\"time in 10million ms\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "3532ff15-2b01-41d5-9386-c9e03ce77134", + "metadata": {}, + "source": [ + "\n", + "\n", + "##### code\n", + "```c\n", + "// Ziad Arafat\n", + "// Created: sep 18 2021\n", + "// Figure 1.10 in the textbook\n", + "\n", + "/**\n", + " * @brief This program will create an array of a specified size and fill it\n", + " * with random numbers between 1 and 3 inclusive. It will then try to count\n", + " * the number of threes in the array using a serial method and a parallel method\n", + " * it will return the counts for each attempt as well as the running times. \n", + " * \n", + " */\n", + "\n", + "/**\n", + "Changelog\n", + "09/17\n", + "- Copied code from the withmutex.c code\n", + "- added dynamic array allocation for the results of the threads\n", + "- removed the mutex lock and unlock\n", + "- modified the thread to indrement the corresponding item in the array instead of global count\n", + "\n", + "\n", + "09/19\n", + "- added free() calls to clean up the memory when done\n", + "\n", + "09/21\n", + "- fixed the way the array is split for the threads\n", + "\n", + "\n", + "*/\n", + "\n", + "#define DEBUG 0 // flag to print debug values\n", + "\n", + "#include \n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "int *A; // Pointer to our array in heap\n", + "int COUNT = 0; // The count of threes we found for the parallel code\n", + "int SEGSIZE; // the size of each chunk per tread\n", + "int NUMOFTHREADS; // the number of threads to spawn\n", + "int *t_results; // an array to store the results from each thread\n", + "int SIZE; // The size of the array\n", + "\n", + "pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable\n", + "\n", + "/**\n", + " * @brief Iterates through a chunk of the A array and counts the number of \n", + " * threes by updating an item in a results array corresponding to the thread.\n", + " * \n", + " * \n", + " * @param index The index of the array chunks re are working with\n", + " * @return void* returns a void pointer to work with pthread\n", + " * \n", + " * @pre The global A variable must be a pointer to an array in heap containing numbers between \n", + " * 1 and 3. index must be a number referring to the index of the chunk we want\n", + " * to work through starting at 0. The global t_results array must be 0 at the\n", + " * corresponding index \n", + " * \n", + " * @post When done the array element corresponding to the thread must have the \n", + " * number of threes we found in our chunk of the array\n", + " */\n", + "void *count3s(void *idx)\n", + "{\n", + " int *index = (int *)idx; // cast the void star input to an int ptr\n", + " int mystart = *index * SEGSIZE; // set the starting point for the thread\n", + " int myend = mystart + SEGSIZE; // set the end point for the thread\n", + "\n", + " if (DEBUG) {\n", + " printf(\"I ran once %d\\n\", *index);\n", + " }\n", + "\n", + " if (DEBUG) {\n", + " printf(\"start is %d\\n\", mystart);\n", + " printf(\"end is %d\\n\", myend);\n", + " }\n", + "\n", + " // iterate through our chunk and increment our array element when a\n", + " // three is found\n", + " for (int i = mystart; i < myend; i++) {\n", + " if (A[i] == 3) {\n", + " t_results[*index]++; // increment result\n", + " }\n", + " }\n", + "\n", + " // printf(\"Threads %d index %d size %d END %d \\n\", NUMOFTHREADS, *index, SIZE, myend);\n", + "\n", + " // When we reach the final index we will calculate the remaining values in the array\n", + " // So that we can add them to the result.\n", + " // This can become quite high once you go past the 50% mark of the array size\n", + " // but our tests will never go that far so this is a pretty good solution.\n", + " if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) {\n", + " if (DEBUG) {\n", + " int remain = SIZE - myend;\n", + " printf(\"Remainder: %d \\n\", remain);\n", + " }\n", + "\n", + " // iterate through the remainder of the array and count the 3s\n", + " for (int i = myend; i < SIZE; i++) {\n", + " if (A[i] == 3) {\n", + " t_results[*index]++;\n", + " }\n", + " }\n", + " }\n", + "\n", + " return (void *)0;\n", + "}\n", + "\n", + "/**\n", + " * @brief This function is used to spawn the appropriate number of threads and \n", + " * also gathers the results of each thread and sums them.\n", + " * \n", + " * @return int containing the time it took\n", + " * \n", + " * @pre Global COUNT is 0. NUMOFTHREADS is greater than 0.\n", + " * SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable\n", + " * points to an array in heap. \n", + " * \n", + " * @post The global COUNT contains the correct three count\n", + " * \n", + " */\n", + "int count3s_parallel()\n", + "{\n", + " // initialize the timer for the parallel work\n", + " struct timespec starttime, endtime;\n", + " int j, k;\n", + "\n", + " // get the start time\n", + " clock_gettime(CLOCK_REALTIME, &starttime); // start the timer\n", + "\n", + " // Created an array allocated at runtime to hold the identifiers for\n", + " // our threads\n", + " pthread_t *t_idents;\n", + " int *t_indices;\n", + "\n", + " // Allocates the space needed for the thread IDs\n", + " t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS));\n", + "\n", + " // in order to fix the \"integer to pointer conversion\" warning\n", + " // i made an array to store the indices for each thread that way\n", + " // i can pass it as a pointer in the for loop\n", + " t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS));\n", + "\n", + " // allocate an array of ints to store the results.\n", + " t_results = (int *)(calloc(NUMOFTHREADS, sizeof(int) * NUMOFTHREADS));\n", + "\n", + " // Create the threads\n", + " for (int i = 0; i < NUMOFTHREADS; i++) {\n", + " t_indices[i] = i;\n", + " pthread_create(&t_idents[i], NULL, count3s,\n", + " (void *)&t_indices[i]);\n", + " }\n", + "\n", + " // wait for all the treads to finish.\n", + " for (int i = 0; i < NUMOFTHREADS; i++) {\n", + " pthread_join(t_idents[i], NULL);\n", + " COUNT += t_results[i];\n", + " }\n", + "\n", + " // free the three arrays we made so we dont leak.\n", + " free(t_idents);\n", + " free(t_indices);\n", + " free(t_results);\n", + "\n", + " // get the end time\n", + " clock_gettime(CLOCK_REALTIME, &endtime);\n", + "\n", + " // calculate duration\n", + " j = endtime.tv_sec - starttime.tv_sec;\n", + " k = endtime.tv_nsec - starttime.tv_nsec;\n", + " j = j * 1000000000 + k;\n", + "\n", + " // return the duration\n", + " return j;\n", + "}\n", + "\n", + "int main(int argc, char const *argv[])\n", + "{\n", + " // this variable stores the time for generating the random numbers.\n", + " time_t t;\n", + "\n", + " // intitilize the random number with the time as the seed\n", + " srand((unsigned)time(&t));\n", + "\n", + " // check the user arguments\n", + " if (argc != 3) {\n", + " // if they are wrong count barf\n", + " printf(\"Not correct arguments \\n\");\n", + " printf(\"Usage %s r of threads> \\n\",\n", + " argv[0]);\n", + " exit(1);\n", + " }\n", + "\n", + " // Convert the user input to integers\n", + " SIZE = atoi(argv[1]);\n", + " NUMOFTHREADS = atoi(argv[2]);\n", + "\n", + " // Set the size of the array chunks to process to the size over the\n", + " // number of threads\n", + " SEGSIZE = SIZE / NUMOFTHREADS;\n", + "\n", + " if (DEBUG) {\n", + " printf(\"number is %s \\n\", argv[1]);\n", + " printf(\"number is %d \\n\", SIZE);\n", + " printf(\"NumThr is %d \\n\", NUMOFTHREADS);\n", + " printf(\"Seg Size is %d \\n\", SEGSIZE);\n", + " }\n", + "\n", + " // Allocate the array based on the input size\n", + " A = (int *)(malloc(sizeof(int) * SIZE));\n", + "\n", + " // if the array is null barf\n", + " if (A == NULL) {\n", + " // printf(\"Austin, we have a problem\");\n", + " perror(\"Austin, we have a problem\");\n", + " exit(1);\n", + " }\n", + "\n", + " // generate random numbers and fill them in the array\n", + " // the numbers are between 1 and 3 inclusive\n", + " for (int i = 0; i < SIZE; i++) {\n", + " // if we are debugging lets make them all threes.\n", + " // that way we can tell if something isnt\n", + " // being counted.\n", + " if (DEBUG) {\n", + " A[i] = 3;\n", + " } else {\n", + " A[i] = rand() % 4;\n", + " }\n", + " }\n", + "\n", + " // Count the threes in parallel\n", + " int parallel_time = count3s_parallel();\n", + "\n", + " // since the COUNT for the parallel is defined before starting the\n", + " // timer i choose to define this one before timer as well.\n", + " int local_count = 0;\n", + "\n", + " // initialize the timer for the parallel work\n", + " struct timespec starttime, endtime;\n", + " int j, k;\n", + " clock_gettime(CLOCK_REALTIME, &starttime); // start the timer\n", + "\n", + " // Count the threes in serial\n", + " for (int i = 0; i < SIZE; i++) {\n", + " if (A[i] == 3) {\n", + " local_count++;\n", + " }\n", + " }\n", + "\n", + " // get end time\n", + " clock_gettime(CLOCK_REALTIME, &endtime);\n", + "\n", + " // calculate duration\n", + " j = endtime.tv_sec - starttime.tv_sec;\n", + " k = endtime.tv_nsec - starttime.tv_nsec;\n", + " j = j * 1000000000 + k;\n", + "\n", + " // free the array\n", + " free(A);\n", + "\n", + " // print the times and counts\n", + " printf(\"Time parallel: %d \\n\", parallel_time);\n", + " printf(\"Time serial: %d \\n\", j);\n", + "\n", + " printf(\"parallel count is %d\\n\", COUNT);\n", + " printf(\"serial count is %d\\n\", local_count);\n", + "\n", + " if (DEBUG) {\n", + " printf(\"SEGSIZE %d \\n\", SEGSIZE);\n", + " }\n", + "\n", + " return 0;\n", + "}\n", + "\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "6e4dbfd5-ffe2-4948-924b-88f5c451b6c0", + "metadata": {}, + "source": [ + "#### Raw console outputs\n", + "\n", + "##### 1 Thread\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> make\n", + "gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o witharray witharray.c\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 1\n", + "Time parallel: 81863555 \n", + "Time serial: 75568341 \n", + "parallel count is 2500620\n", + "serial count is 2500620\n", + "SEGSIZE 10000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 1\n", + "Time parallel: 79677104 \n", + "Time serial: 75715147 \n", + "parallel count is 2499935\n", + "serial count is 2499935\n", + "SEGSIZE 10000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 1\n", + "Time parallel: 80084768 \n", + "Time serial: 76378151 \n", + "parallel count is 2498190\n", + "serial count is 2498190\n", + "SEGSIZE 10000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 1\n", + "Time parallel: 83075721 \n", + "Time serial: 78784952 \n", + "parallel count is 2499420\n", + "serial count is 2499420\n", + "SEGSIZE 10000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 1\n", + "Time parallel: 79219853 \n", + "Time serial: 80265097 \n", + "parallel count is 2503792\n", + "serial count is 2503792\n", + "SEGSIZE 10000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "```\n", + "\n", + "##### 2 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 2\n", + "^[[ATime parallel: 117217005 \n", + "Time serial: 76232392 \n", + "parallel count is 2500931\n", + "serial count is 2500931\n", + "SEGSIZE 5000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 2\n", + "Time parallel: 117227900 \n", + "Time serial: 75723948 \n", + "parallel count is 2499572\n", + "serial count is 2499572\n", + "SEGSIZE 5000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 2\n", + "Time parallel: 125809927 \n", + "Time serial: 76198239 \n", + "parallel count is 2499572\n", + "serial count is 2499572\n", + "SEGSIZE 5000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 2\n", + "^[[A\n", + "Time parallel: 117415982 \n", + "Time serial: 76056322 \n", + "parallel count is 2497775\n", + "serial count is 2497775\n", + "SEGSIZE 5000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 2\n", + "Time parallel: 116840839 \n", + "Time serial: 76606811 \n", + "parallel count is 2497775\n", + "serial count is 2497775\n", + "SEGSIZE 5000000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "```\n", + "\n", + "##### 4 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 4\n", + "Time parallel: 119560039 \n", + "Time serial: 75835135 \n", + "parallel count is 2498552\n", + "serial count is 2498552\n", + "SEGSIZE 2500000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 4\n", + "Time parallel: 110999175 \n", + "Time serial: 75637135 \n", + "parallel count is 2502155\n", + "serial count is 2502155\n", + "SEGSIZE 2500000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 4\n", + "Time parallel: 120659063 \n", + "Time serial: 75797630 \n", + "parallel count is 2502155\n", + "serial count is 2502155\n", + "SEGSIZE 2500000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 4\n", + "Time parallel: 105918640 \n", + "Time serial: 75691681 \n", + "parallel count is 2500763\n", + "serial count is 2500763\n", + "SEGSIZE 2500000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 4\n", + "Time parallel: 118025767 \n", + "Time serial: 75826754 \n", + "parallel count is 2501575\n", + "serial count is 2501575\n", + "SEGSIZE 2500000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "\n", + "```\n", + "\n", + "##### 8 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 8\n", + "Time parallel: 75531675 \n", + "Time serial: 76051224 \n", + "parallel count is 2500300\n", + "serial count is 2500300\n", + "SEGSIZE 1250000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 8\n", + "Time parallel: 75182817 \n", + "Time serial: 75753420 \n", + "parallel count is 2499436\n", + "serial count is 2499436\n", + "SEGSIZE 1250000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 8\n", + "Time parallel: 76046405 \n", + "Time serial: 76348748 \n", + "parallel count is 2501044\n", + "serial count is 2501044\n", + "SEGSIZE 1250000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 8\n", + "Time parallel: 75545853 \n", + "Time serial: 76008551 \n", + "parallel count is 2499779\n", + "serial count is 2499779\n", + "SEGSIZE 1250000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 8\n", + "Time parallel: 75960779 \n", + "Time serial: 78840754 \n", + "parallel count is 2499779\n", + "serial count is 2499779\n", + "SEGSIZE 1250000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "\n", + "```\n", + "\n", + "##### 16 threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 16\n", + "Time parallel: 51618507 \n", + "Time serial: 79680457 \n", + "parallel count is 2500182\n", + "serial count is 2500182\n", + "SEGSIZE 625000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 16\n", + "Time parallel: 52043701 \n", + "Time serial: 75934658 \n", + "parallel count is 2502307\n", + "serial count is 2502307\n", + "SEGSIZE 625000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 16\n", + "Time parallel: 51582748 \n", + "Time serial: 75689934 \n", + "parallel count is 2499453\n", + "serial count is 2499453\n", + "SEGSIZE 625000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 16\n", + "Time parallel: 52321250 \n", + "Time serial: 76262634 \n", + "parallel count is 2499453\n", + "serial count is 2499453\n", + "SEGSIZE 625000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 16\n", + "Time parallel: 51572481 \n", + "Time serial: 76141109 \n", + "parallel count is 2498729\n", + "serial count is 2498729\n", + "SEGSIZE 625000 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "\n", + "```\n", + "\n", + "##### 32 threads\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 32\n", + "Time parallel: 52033155 \n", + "Time serial: 75630360 \n", + "parallel count is 2502265\n", + "serial count is 2502265\n", + "SEGSIZE 312500 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 32\n", + "Time parallel: 40560746 \n", + "Time serial: 75752932 \n", + "parallel count is 2496956\n", + "serial count is 2496956\n", + "SEGSIZE 312500 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 32\n", + "Time parallel: 33518090 \n", + "Time serial: 79269021 \n", + "parallel count is 2499218\n", + "serial count is 2499218\n", + "SEGSIZE 312500 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 32\n", + "Time parallel: 42830797 \n", + "Time serial: 75999611 \n", + "parallel count is 2499218\n", + "serial count is 2499218\n", + "SEGSIZE 312500 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 32\n", + "Time parallel: 38449513 \n", + "Time serial: 76038163 \n", + "parallel count is 2498024\n", + "serial count is 2498024\n", + "SEGSIZE 312500 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "```\n", + "\n", + "##### 64 threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 64\n", + "Time parallel: 23108945 \n", + "Time serial: 76429135 \n", + "parallel count is 2501086\n", + "serial count is 2501086\n", + "SEGSIZE 156250 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 64\n", + "Time parallel: 32038014 \n", + "Time serial: 78676976 \n", + "parallel count is 2501086\n", + "serial count is 2501086\n", + "SEGSIZE 156250 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 64\n", + "Time parallel: 29387118 \n", + "Time serial: 75795394 \n", + "parallel count is 2499983\n", + "serial count is 2499983\n", + "SEGSIZE 156250 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 64\n", + "Time parallel: 21765129 \n", + "Time serial: 75698106 \n", + "parallel count is 2499590\n", + "serial count is 2499590\n", + "SEGSIZE 156250 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 64\n", + "Time parallel: 26043536 \n", + "Time serial: 76504843 \n", + "parallel count is 2499590\n", + "serial count is 2499590\n", + "SEGSIZE 156250 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "```\n", + "\n", + "##### 128 threads\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 128\n", + "Time parallel: 27093181 \n", + "Time serial: 75747694 \n", + "parallel count is 2499556\n", + "serial count is 2499556\n", + "SEGSIZE 78125 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 128\n", + "Time parallel: 23288786 \n", + "Time serial: 76220100 \n", + "parallel count is 2501938\n", + "serial count is 2501938\n", + "SEGSIZE 78125 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 128\n", + "Time parallel: 28004540 \n", + "Time serial: 84610064 \n", + "parallel count is 2498595\n", + "serial count is 2498595\n", + "SEGSIZE 78125 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 128\n", + "Time parallel: 24022050 \n", + "Time serial: 76480189 \n", + "parallel count is 2498595\n", + "serial count is 2498595\n", + "SEGSIZE 78125 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 128\n", + "Time parallel: 26019790 \n", + "Time serial: 76096691 \n", + "parallel count is 2500116\n", + "serial count is 2500116\n", + "SEGSIZE 78125 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "```\n", + "\n", + "\n", + "##### 256 threads\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 256\n", + "Time parallel: 30797702 \n", + "Time serial: 76585020 \n", + "parallel count is 2500664\n", + "serial count is 2500664\n", + "SEGSIZE 39062 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 256\n", + "Time parallel: 30520153 \n", + "Time serial: 76308938 \n", + "parallel count is 2502137\n", + "serial count is 2502137\n", + "SEGSIZE 39062 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 256\n", + "Time parallel: 29454864 \n", + "Time serial: 76265916 \n", + "parallel count is 2501344\n", + "serial count is 2501344\n", + "SEGSIZE 39062 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 256\n", + "Time parallel: 32954541 \n", + "Time serial: 76108284 \n", + "parallel count is 2498982\n", + "serial count is 2498982\n", + "SEGSIZE 39062 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 256\n", + "Time parallel: 33016071 \n", + "Time serial: 75821655 \n", + "parallel count is 2498982\n", + "serial count is 2498982\n", + "SEGSIZE 39062 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "```\n", + "\n", + "##### 512 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 512\n", + "^[[ATime parallel: 48074062 \n", + "Time serial: 76889948 \n", + "parallel count is 2503817\n", + "serial count is 2503817\n", + "SEGSIZE 19531 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 512\n", + "Time parallel: 44829655 \n", + "Time serial: 76344138 \n", + "parallel count is 2500075\n", + "serial count is 2500075\n", + "SEGSIZE 19531 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 512\n", + "Time parallel: 45507953 \n", + "Time serial: 75874944 \n", + "parallel count is 2500075\n", + "serial count is 2500075\n", + "SEGSIZE 19531 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 512\n", + "Time parallel: 49365148 \n", + "Time serial: 76632722 \n", + "parallel count is 2499084\n", + "serial count is 2499084\n", + "SEGSIZE 19531 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> ./witharray 10000000 512\n", + "Time parallel: 44905711 \n", + "Time serial: 77069929 \n", + "parallel count is 2499084\n", + "serial count is 2499084\n", + "SEGSIZE 19531 \n", + "doge@godemperordoge1998 ~/D/p/witharray (main)> \n", + "\n", + "\n", + "```\n" + ] + }, + { + "cell_type": "markdown", + "id": "abbd3c4f-4e84-49c8-946f-4bc59abd7f90", + "metadata": {}, + "source": [ + "## Setup 4\n", + "- Each thread increments it's own count variable in an array **with padding**\n", + "\n", + "##### Raw Data Processing and Graphing" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7204187f-faeb-4184-ba4e-3ae79a5a3f86", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Text(0, 0.5, 'time in 10million ms')" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXgAAAEfCAYAAABLbN1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRw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Ua8VhxO89RT9gU7PvCdp0/KHUjFO8NYtTvDWU1wIfEjSHEnfS7f1k3SbpGckzVC6BpqkBZK+K2k2ybJun5T0qKTZkm6V1C8t9/8l/VnSPElTS47fW9KTkp4ETm8vIEnnSpqblr0w3TZK0qx0sfdfStom3f6ApPr09baSFqSvJ6QLW8+U9FdJF6XbLwQ2Tz/vjPL/c9rGwAneeorzgL9HxKiIOCfdtifwZZJFjXcC9ispvyhdCOJ3wGTg4PR9A9Cymv1lEfHRiNgN2Bw4PN1+PXBmROzRXjCSxgJHAqPTchelu34KnJsujjGXZEHoDRkFjCNZdH2cpB0i4jzg3fTz/nuGc5itxwneerI/RURjOlPfHGBYyb6b0+ePkfwB+KOkOSTT3rbM4PcJSY+lKxmNAUZK6g/0j4iH0jI3tFP3wcD16dzpRMRb6bzp/SPiwbTMdOCADJ/j9xGxJCJWAE+XxGfWLb7RyXqylSWv17Duz3PLWpcC7ouI40oPlNQHuAKoj4iXJU0B+uQY62reb1C1rqejz2HWZW7BW0/RBGzZheNmAftJ2hlA0hbp0m4tSfbNtE/+MwARsRhYLGn/dH973SP3AZ+X1Dc974B0IYq3Jf1zWuazQEtrfgHvr5L0mYyxr5LUO2NZs/U4wVuPEBGLSLpZ5pVcZM1y3EJgAnBTOvHTo8CuaSK/BpgH/Ab4c8lhnwcuT7t01M55ZwK/AhrScmenuz4HfC+taxTwjXT794FTJT0BbJsx/KnAU77Ial3lqQrMzArKLXgzs4JygjczKygneDOzgnKCNzMrKCd4M7OCcoI3MysoJ3gzs4L6X3rFo2ThBx1hAAAAAElFTkSuQmCC\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "# Data Processing\n", + "\n", + "ser = [76142799.4, 77717943.8, 76086493.6, 76067440.6, 76296925.4, 76344599, 76862234.8, 76255383.8]\n", + "par = [78454322.4, 40209206.6, 20134348.6, 10790813.6, 12481699.4, 12199778.2, 13109907.8, 15661670.6]\n", + "\n", + "val_dict = {}\n", + "\n", + "for k, v in enumerate(par):\n", + " val_dict[str(2**k)]={\n", + " \"serial\": ser[k],\n", + " \"parall\": par[k]\n", + " }\n", + "\n", + "df1 = pandas.DataFrame.from_dict(val_dict, orient='index')\n", + "\n", + "\n", + "\n", + "df1.plot.bar(title='Setup 4')\n", + "plt.xlabel(\"thread count\")\n", + "plt.ylabel(\"time in 10million ms\")" + ] + }, + { + "cell_type": "markdown", + "id": "da13f2cb-65e6-42f9-a9bd-183cb6339d54", + "metadata": {}, + "source": [ + "\n", + "\n", + "##### code\n", + "```c\n", + "// Ziad Arafat\n", + "// Created: Sep 19 2021\n", + "// Figure 1.11 in the textbook\n", + "\n", + "/**\n", + " * @brief This program will create an array of a specified size and fill it\n", + " * with random numbers between 1 and 3 inclusive. It will then try to count\n", + " * the number of threes in the array using a serial method and a parallel method\n", + " * it will return the counts for each attempt as well as the running times. \n", + " * \n", + " * In this version we will create a struct called padded_int which will contain\n", + " * the count as well as a padding of chars set to fill up the chache line of \n", + " * the maching we run it on.\n", + " */\n", + "\n", + "/**\n", + "Changelog\n", + "\n", + "09/18\n", + "- copied code from witharray.c\n", + "- created the struct padded_int with the appropriate char padding\n", + "- modified the results array to be an array of our padded_int\n", + "- modified the thread to correctly access and increment the padded_ints\n", + "- \n", + "\n", + "9/19\n", + "- added free() calls to clean up the memory when done\n", + "\n", + "09/21\n", + "- fixed the way the array is split for the threads by making the size global\n", + "\n", + "BUGFIX: The way the array was bieng split was incorrect because unless the array\n", + "size was a multiple of the thread count there would be a remaining chunk of\n", + "the array. In fact if the thread count is more than half the array size this\n", + "chunk could be massive. The fix involved adding an extra loop that gathered the\n", + "remaining array elements on the last run. I feel like there is a more elegant\n", + "solution but this seems to work well for the purpose of this program? \n", + "\n", + "*/\n", + "\n", + "#define DEBUG 0 // flag to print debug values\n", + "\n", + "#include \n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "int *A; // Pointer to our array in heap\n", + "int COUNT = 0; // The count of threes we found for the parallel code\n", + "int SEGSIZE; // the size of each chunk per tread\n", + "int NUMOFTHREADS; // the number of threads to spawn\n", + "int SIZE; // size of the array\n", + "\n", + "// This is a struct that will hold our data in the array for padding.\n", + "struct padded_int {\n", + " // We need 64B of padding. Because our processor has 64B cache lines\n", + " int value; // an int is 4 bytes\n", + " char padding[60]; // now we need 64-4 more bytes so we make 60 chars\n", + "};\n", + "\n", + "struct padded_int *t_results; // an array to store the results from each thread\n", + "\n", + "pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable\n", + "\n", + "/**\n", + " * @brief Iterates through a chunk of the A array and counts the number of \n", + " * threes by updating the COUNT variable\n", + " * \n", + " * \n", + " * @param index The index of the array chunks re are working with\n", + " * @return void* returns a void pointer to work with pthread\n", + " * \n", + " * @pre The global A variable must be a pointer to an array in heap containing numbers between \n", + " * 1 and 3. index must be a number referring to the index of the chunk we want\n", + " * to work through starting at 0. The global COUNT variable must contain the\n", + " * number of threes we have already counted\n", + " * \n", + " * @post When done the value attribute of the padded int in the array element \n", + " * corresponding to the thread must have the correct number of threes we found\n", + " * in our chunk of the array\n", + " */\n", + "void *count3s(void *idx)\n", + "{\n", + " int *index = (int *)idx; // cast the void star input to an int ptr\n", + " int mystart = *index * SEGSIZE; // set the starting point for the thread\n", + " int myend = mystart + SEGSIZE; // set the end point for the thread\n", + "\n", + " if (DEBUG) {\n", + " printf(\"I ran once %d\\n\", *index);\n", + " }\n", + " \n", + " // this temporary variable is here because i am scared of pointers and\n", + " // structs so this just makes sure i dont break things.\n", + " int valueforthread = 0;\n", + "\n", + " if (DEBUG) {\n", + " printf(\"start is %d\\n\", mystart);\n", + " printf(\"end is %d\\n\", myend);\n", + " }\n", + "\n", + " for (int i = mystart; i < myend; i++) {\n", + " if (A[i] == 3) {\n", + " valueforthread++; // increment result\n", + " }\n", + " }\n", + "\n", + " // When we reach the final index we will calculate the remaining values in the array\n", + " // So that we can add them to the result.\n", + " // This can become quite high once you go past the 50% mark of the array size\n", + " // but our tests will never go that far so this is a pretty good solution.\n", + " if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) {\n", + " // int remain = SIZE - myend;\n", + " // printf(\"Remainder: %d \\n\", remain);\n", + " // printf(\"ACTIVE \\n\");\n", + " for (int i = myend; i < SIZE; i++) {\n", + " if (A[i] == 3) {\n", + " valueforthread++;\n", + " }\n", + " }\n", + " }\n", + "\n", + " if (DEBUG) {\n", + " printf(\"value counted: %d \\n\", valueforthread);\n", + " }\n", + "\n", + " // I am once again scared of pointers so i manually set it to 0\n", + " // and add on the value.\n", + " t_results[*index].value = 0;\n", + " t_results[*index].value += valueforthread;\n", + "\n", + " return (void *)0;\n", + "}\n", + "\n", + "/**\n", + " * @brief This function is used to spawn the appropriate number of threads and\n", + " * also gathers the results of each thread and sums them.\n", + " * \n", + " * @return int containing the time it took\n", + " * \n", + " * @pre The global COUNT variable is 0. NUMOFTHREADS is greater than 0.\n", + " * SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable\n", + " * points to an array in heap. \n", + " * \n", + " * @post The global COUNT contains the correct three count\n", + " * \n", + " */\n", + "int count3s_parallel()\n", + "{\n", + " // initialize the timer for the parallel work\n", + " struct timespec starttime, endtime;\n", + " int j, k;\n", + "\n", + " // get the start time\n", + " clock_gettime(CLOCK_REALTIME, &starttime); // start the timer\n", + "\n", + " // Created an array allocated at runtime to hold the identifiers for\n", + " // our threads\n", + " pthread_t *t_idents;\n", + " int *t_indices;\n", + "\n", + " // Allocates the space needed for the thread IDs\n", + " t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS));\n", + "\n", + " // in order to fix the \"integer to pointer conversion\" warning\n", + " // i made an array to store the indices for each thread that way\n", + " // i can pass it as a pointer in the for loop\n", + " t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS));\n", + "\n", + " // We will initialize the padded ints manually in the thread because\n", + " // I dont know how calloc will behave with a struct.\n", + " t_results = (struct padded_int *)(malloc(sizeof(struct padded_int) *\n", + " NUMOFTHREADS));\n", + "\n", + " // Create the threads\n", + " for (int i = 0; i < NUMOFTHREADS; i++) {\n", + " t_indices[i] = i;\n", + " pthread_create(&t_idents[i], NULL, count3s,\n", + " (void *)&t_indices[i]);\n", + " }\n", + " COUNT = 0;\n", + " // wait for all the treads to finish.\n", + " for (int i = 0; i < NUMOFTHREADS; i++) {\n", + " pthread_join(t_idents[i], NULL);\n", + " COUNT += t_results[i].value;\n", + " }\n", + "\n", + " // free the three arrays we made so we dont leak.\n", + " free(t_idents);\n", + " free(t_indices);\n", + " free(t_results);\n", + "\n", + " // get the end time\n", + " clock_gettime(CLOCK_REALTIME, &endtime);\n", + "\n", + " // calculate duration\n", + " j = endtime.tv_sec - starttime.tv_sec;\n", + " k = endtime.tv_nsec - starttime.tv_nsec;\n", + " j = j * 1000000000 + k;\n", + "\n", + " // return the duration\n", + " return j;\n", + "}\n", + "\n", + "int main(int argc, char const *argv[])\n", + "{\n", + " // this variable stores the time for generating the random numbers.\n", + " time_t t;\n", + "\n", + " // intitilize the random number with the time as the seed\n", + " srand((unsigned)time(&t));\n", + "\n", + " if (argc != 3) {\n", + " printf(\"Not correct arguments \\n\");\n", + " printf(\"Usage %s \\n\",\n", + " argv[0]);\n", + " exit(1);\n", + " }\n", + "\n", + " // Convert the user input to integers\n", + " SIZE = atoi(argv[1]);\n", + " NUMOFTHREADS = atoi(argv[2]);\n", + "\n", + " // Set the size of the array chunks to process to the size over the\n", + " // number of threads\n", + " SEGSIZE = SIZE / NUMOFTHREADS;\n", + "\n", + " if (DEBUG) {\n", + " printf(\"number is %s \\n\", argv[1]);\n", + " printf(\"number is %d \\n\", SIZE);\n", + " printf(\"NumThr is %d \\n\", NUMOFTHREADS);\n", + " printf(\"Seg Size is %d \\n\", SEGSIZE);\n", + " }\n", + "\n", + " // Allocate the array based on the input size\n", + " A = (int *)(malloc(sizeof(int) * SIZE));\n", + "\n", + " // if the array is null barf\n", + " if (A == NULL) {\n", + " // printf(\"Austin, we have a problem\");\n", + " perror(\"Austin, we have a problem\");\n", + " exit(1);\n", + " }\n", + " // generate random numbers and fill them in the array\n", + " // the numbers are between 1 and 3 inclusive\n", + " for (int i = 0; i < SIZE; i++) {\n", + " // if we are debugging lets make them all threes.\n", + " // that way we can tell if something isnt\n", + " // being counted.\n", + " if (DEBUG) {\n", + " A[i] = 3;\n", + " } else {\n", + " A[i] = rand() % 4;\n", + " }\n", + " }\n", + "\n", + " // Count the threes in parallel\n", + " int parallel_time = count3s_parallel();\n", + "\n", + " // since the COUNT for the parallel is defined before starting the\n", + " // timer i choose to define this one before timer as well.\n", + " int local_count = 0;\n", + "\n", + " // initialize the timer for the parallel work\n", + " struct timespec starttime, endtime;\n", + " int j, k;\n", + " clock_gettime(CLOCK_REALTIME, &starttime); // start the timer\n", + "\n", + " // Count the threes in serial\n", + " for (int i = 0; i < SIZE; i++) {\n", + " if (A[i] == 3) {\n", + " local_count++;\n", + " }\n", + " }\n", + "\n", + " // get end time\n", + " clock_gettime(CLOCK_REALTIME, &endtime);\n", + "\n", + " // calculate duration\n", + " j = endtime.tv_sec - starttime.tv_sec;\n", + " k = endtime.tv_nsec - starttime.tv_nsec;\n", + " j = j * 1000000000 + k;\n", + "\n", + " // free the array\n", + " free(A);\n", + "\n", + " // print the times and counts\n", + " printf(\"Time parallel: %d \\n\", parallel_time);\n", + " printf(\"Time serial: %d \\n\", j);\n", + "\n", + " printf(\"parallel count is %d\\n\", COUNT);\n", + " printf(\"serial count is %d\\n\", local_count);\n", + "\n", + " return 0;\n", + "}\n", + "\n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "03ce99b3-b493-4b4e-ad7b-4e1543ab8fe6", + "metadata": {}, + "source": [ + "#### Raw console outputs\n", + "\n", + "##### 1 Thread\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> make\n", + "gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o padded padded.c\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1\n", + "Time parallel: 79589173 \n", + "Time serial: 76582925 \n", + "parallel count is 2499295\n", + "serial count is 2499295\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1\n", + "Time parallel: 77817021 \n", + "Time serial: 76191186 \n", + "parallel count is 2501600\n", + "serial count is 2501600\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1\n", + "Time parallel: 78369396 \n", + "Time serial: 76075458 \n", + "parallel count is 2501212\n", + "serial count is 2501212\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1\n", + "Time parallel: 77878131 \n", + "Time serial: 75789878 \n", + "parallel count is 2497788\n", + "serial count is 2497788\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 1\n", + "Time parallel: 78617891 \n", + "Time serial: 76074550 \n", + "parallel count is 2499979\n", + "serial count is 2499979\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> \n", + "```\n", + "\n", + "##### 2 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2\n", + "Time parallel: 39898720 \n", + "Time serial: 76881427 \n", + "parallel count is 2498665\n", + "serial count is 2498665\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2\n", + "Time parallel: 38993717 \n", + "Time serial: 75975306 \n", + "parallel count is 2498665\n", + "serial count is 2498665\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2\n", + "Time parallel: 43756962 \n", + "Time serial: 75922017 \n", + "parallel count is 2499470\n", + "serial count is 2499470\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2\n", + "Time parallel: 39628155 \n", + "Time serial: 75874106 \n", + "parallel count is 2499470\n", + "serial count is 2499470\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 2\n", + "Time parallel: 38768479 \n", + "Time serial: 83936863 \n", + "parallel count is 2499249\n", + "serial count is 2499249\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> \n", + "\n", + "\n", + "```\n", + "\n", + "##### 4 Threads\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4\n", + "Time parallel: 20135731 \n", + "Time serial: 76379757 \n", + "parallel count is 2500818\n", + "serial count is 2500818\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4\n", + "Time parallel: 20184131 \n", + "Time serial: 76066309 \n", + "parallel count is 2500619\n", + "serial count is 2500619\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4\n", + "Time parallel: 20012671 \n", + "Time serial: 76159618 \n", + "parallel count is 2500619\n", + "serial count is 2500619\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4\n", + "Time parallel: 20407135 \n", + "Time serial: 76091592 \n", + "parallel count is 2501896\n", + "serial count is 2501896\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 4\n", + "Time parallel: 19932075 \n", + "Time serial: 75735192 \n", + "parallel count is 2501896\n", + "serial count is 2501896\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> \n", + "\n", + "```\n", + "\n", + "##### 8 Threads\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8\n", + "Time parallel: 10802910 \n", + "Time serial: 76379477 \n", + "parallel count is 2499488\n", + "serial count is 2499488\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8\n", + "Time parallel: 10938262 \n", + "Time serial: 75819630 \n", + "parallel count is 2499488\n", + "serial count is 2499488\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8\n", + "Time parallel: 10780422 \n", + "Time serial: 75986271 \n", + "parallel count is 2499970\n", + "serial count is 2499970\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8\n", + "Time parallel: 10738028 \n", + "Time serial: 75738055 \n", + "parallel count is 2499970\n", + "serial count is 2499970\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 8\n", + "Time parallel: 10694446 \n", + "Time serial: 76413770 \n", + "parallel count is 2498866\n", + "serial count is 2498866\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> \n", + "```\n", + "\n", + "##### 16 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16\n", + "Time parallel: 15428848 \n", + "Time serial: 76461611 \n", + "parallel count is 2499356\n", + "serial count is 2499356\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16\n", + "Time parallel: 10718332 \n", + "Time serial: 76038093 \n", + "parallel count is 2497083\n", + "serial count is 2497083\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16\n", + "Time parallel: 11129069 \n", + "Time serial: 76266824 \n", + "parallel count is 2497083\n", + "serial count is 2497083\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16\n", + "Time parallel: 12962264 \n", + "Time serial: 76701516 \n", + "parallel count is 2500337\n", + "serial count is 2500337\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 16\n", + "Time parallel: 12169984 \n", + "Time serial: 76016583 \n", + "parallel count is 2500337\n", + "serial count is 2500337\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> \n", + "\n", + "```\n", + "\n", + "##### 32 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32\n", + "Time parallel: 13191343 \n", + "Time serial: 76977528 \n", + "parallel count is 2501573\n", + "serial count is 2501573\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32\n", + "Time parallel: 11433019 \n", + "Time serial: 75182119 \n", + "parallel count is 2499230\n", + "serial count is 2499230\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32\n", + "Time parallel: 11259742 \n", + "Time serial: 76361110 \n", + "parallel count is 2499230\n", + "serial count is 2499230\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32\n", + "Time parallel: 13108860 \n", + "Time serial: 76463636 \n", + "parallel count is 2497643\n", + "serial count is 2497643\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 32\n", + "Time parallel: 12005927 \n", + "Time serial: 76738602 \n", + "parallel count is 2500608\n", + "serial count is 2500608\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> \n", + "\n", + "\n", + "\n", + "```\n", + "\n", + "##### 64 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64\n", + "Time parallel: 13443889 \n", + "Time serial: 75998703 \n", + "parallel count is 2501406\n", + "serial count is 2501406\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64\n", + "Time parallel: 13066536 \n", + "Time serial: 80285631 \n", + "parallel count is 2498365\n", + "serial count is 2498365\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64\n", + "Time parallel: 12961145 \n", + "Time serial: 76057230 \n", + "parallel count is 2497858\n", + "serial count is 2497858\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64\n", + "Time parallel: 13297153 \n", + "Time serial: 76248665 \n", + "parallel count is 2497858\n", + "serial count is 2497858\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 64\n", + "Time parallel: 12780816 \n", + "Time serial: 75720945 \n", + "parallel count is 2500354\n", + "serial count is 2500354\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> \n", + "```\n", + "\n", + "##### 128 Threads\n", + "\n", + "```bash\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128\n", + "Time parallel: 14702009 \n", + "Time serial: 75864468 \n", + "parallel count is 2497320\n", + "serial count is 2497320\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128\n", + "Time parallel: 15708353 \n", + "Time serial: 76406018 \n", + "parallel count is 2497320\n", + "serial count is 2497320\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128\n", + "Time parallel: 14599762 \n", + "Time serial: 76125185 \n", + "parallel count is 2499108\n", + "serial count is 2499108\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128\n", + "Time parallel: 18146093 \n", + "Time serial: 76192373 \n", + "parallel count is 2496782\n", + "serial count is 2496782\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> ./padded 10000000 128\n", + "Time parallel: 15152136 \n", + "Time serial: 76688875 \n", + "parallel count is 2496782\n", + "serial count is 2496782\n", + "doge@godemperordoge1998 ~/D/p/padded (main)> \n", + "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "99121f4d-fb41-4c23-aa14-44b403914cc6", + "metadata": {}, + "source": [ + "#### Python Code For more data processing\n", + "\n", + "To quickly calculate average values for each thread count i wrote this python program that parses the console output and prints out the averages in sequence. \n", + "\n", + "```python\n", + "\n", + "from statistics import mean\n", + "par = []\n", + "ser = []\n", + "\n", + "avgp = []\n", + "avgs = []\n", + "\n", + "threads = 0\n", + "vals = 0\n", + "\n", + "\n", + "with open('./outs.txt') as reader:\n", + " lines = reader.readlines()\n", + "\n", + " for line in lines:\n", + " if vals == 5:\n", + " avgp.append(mean(par))\n", + " avgs.append(mean(ser))\n", + " par=[]\n", + " ser=[]\n", + " vals = 0\n", + " threads += 1\n", + " word = line.split(' ')\n", + " if 'parallel:' in word:\n", + " par.append(int(word[2]))\n", + " if 'serial:' in word:\n", + " ser.append(int(word[4]))\n", + " vals += 1\n", + "\n", + "print(\"ser: \", avgs, \"par: \", avgp, threads)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a378fc14-fe3a-4dff-8a87-06fc6829ba33", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Program2/readthem.py b/Program2/readthem.py new file mode 100644 index 0000000..b2a2a8b --- /dev/null +++ b/Program2/readthem.py @@ -0,0 +1,39 @@ +""" +This simple program is to quickly parse out the outputs so we can get the average +times of the runs. outs.txt must contain the raw console outputs + +""" + +from statistics import mean +par = [] +ser = [] + +avgp = [] +avgs = [] + +threads = 0 +vals = 0 + + +with open('./outs.txt') as reader: + lines = reader.readlines() + + for line in lines: + if vals == 5: + avgp.append(mean(par)) + avgs.append(mean(ser)) + par=[] + ser=[] + vals = 0 + threads += 1 + word = line.split(' ') + if 'parallel:' in word: + par.append(int(word[2])) + if 'serial:' in word: + ser.append(int(word[4])) + vals += 1 + +print("ser: ", avgs, "par: ", avgp, threads) + + + diff --git a/Program2/witharray/.clang-format b/Program2/witharray/.clang-format new file mode 100644 index 0000000..77cecdd --- /dev/null +++ b/Program2/witharray/.clang-format @@ -0,0 +1,550 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# clang-format configuration file. Intended for clang-format >= 4. +# +# For more information, see: +# +# Documentation/process/clang-format.rst +# https://clang.llvm.org/docs/ClangFormat.html +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +# +--- +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left # Unknown to clang-format-4.0 +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false # Unknown to clang-format-5.0 + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true # Unknown to clang-format-4.0 + SplitEmptyRecord: true # Unknown to clang-format-4.0 + SplitEmptyNamespace: true # Unknown to clang-format-4.0 +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false # Unknown to clang-format-4.0 +BreakBeforeTernaryOperators: false +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeComma # Unknown to clang-format-4.0 +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false # Unknown to clang-format-4.0 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false # Unknown to clang-format-4.0 + +# Taken from: +# git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' include/ \ +# | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$, - '\1'," \ +# | sort | uniq +ForEachMacros: + - 'apei_estatus_for_each_section' + - 'ata_for_each_dev' + - 'ata_for_each_link' + - '__ata_qc_for_each' + - 'ata_qc_for_each' + - 'ata_qc_for_each_raw' + - 'ata_qc_for_each_with_internal' + - 'ax25_for_each' + - 'ax25_uid_for_each' + - '__bio_for_each_bvec' + - 'bio_for_each_bvec' + - 'bio_for_each_bvec_all' + - 'bio_for_each_integrity_vec' + - '__bio_for_each_segment' + - 'bio_for_each_segment' + - 'bio_for_each_segment_all' + - 'bio_list_for_each' + - 'bip_for_each_vec' + - 'bitmap_for_each_clear_region' + - 'bitmap_for_each_set_region' + - 'blkg_for_each_descendant_post' + - 'blkg_for_each_descendant_pre' + - 'blk_queue_for_each_rl' + - 'bond_for_each_slave' + - 'bond_for_each_slave_rcu' + - 'bpf_for_each_spilled_reg' + - 'btree_for_each_safe128' + - 'btree_for_each_safe32' + - 'btree_for_each_safe64' + - 'btree_for_each_safel' + - 'card_for_each_dev' + - 'cgroup_taskset_for_each' + - 'cgroup_taskset_for_each_leader' + - 'cpufreq_for_each_entry' + - 'cpufreq_for_each_entry_idx' + - 'cpufreq_for_each_valid_entry' + - 'cpufreq_for_each_valid_entry_idx' + - 'css_for_each_child' + - 'css_for_each_descendant_post' + - 'css_for_each_descendant_pre' + - 'device_for_each_child_node' + - 'dma_fence_chain_for_each' + - 'do_for_each_ftrace_op' + - 'drm_atomic_crtc_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane_state' + - 'drm_atomic_for_each_plane_damage' + - 'drm_client_for_each_connector_iter' + - 'drm_client_for_each_modeset' + - 'drm_connector_for_each_possible_encoder' + - 'drm_for_each_bridge_in_chain' + - 'drm_for_each_connector_iter' + - 'drm_for_each_crtc' + - 'drm_for_each_crtc_reverse' + - 'drm_for_each_encoder' + - 'drm_for_each_encoder_mask' + - 'drm_for_each_fb' + - 'drm_for_each_legacy_plane' + - 'drm_for_each_plane' + - 'drm_for_each_plane_mask' + - 'drm_for_each_privobj' + - 'drm_mm_for_each_hole' + - 'drm_mm_for_each_node' + - 'drm_mm_for_each_node_in_range' + - 'drm_mm_for_each_node_safe' + - 'flow_action_for_each' + - 'for_each_active_dev_scope' + - 'for_each_active_drhd_unit' + - 'for_each_active_iommu' + - 'for_each_aggr_pgid' + - 'for_each_available_child_of_node' + - 'for_each_bio' + - 'for_each_board_func_rsrc' + - 'for_each_bvec' + - 'for_each_card_auxs' + - 'for_each_card_auxs_safe' + - 'for_each_card_components' + - 'for_each_card_dapms' + - 'for_each_card_pre_auxs' + - 'for_each_card_prelinks' + - 'for_each_card_rtds' + - 'for_each_card_rtds_safe' + - 'for_each_card_widgets' + - 'for_each_card_widgets_safe' + - 'for_each_cgroup_storage_type' + - 'for_each_child_of_node' + - 'for_each_clear_bit' + - 'for_each_clear_bit_from' + - 'for_each_cmsghdr' + - 'for_each_compatible_node' + - 'for_each_component_dais' + - 'for_each_component_dais_safe' + - 'for_each_comp_order' + - 'for_each_console' + - 'for_each_cpu' + - 'for_each_cpu_and' + - 'for_each_cpu_not' + - 'for_each_cpu_wrap' + - 'for_each_dapm_widgets' + - 'for_each_dev_addr' + - 'for_each_dev_scope' + - 'for_each_displayid_db' + - 'for_each_dma_cap_mask' + - 'for_each_dpcm_be' + - 'for_each_dpcm_be_rollback' + - 'for_each_dpcm_be_safe' + - 'for_each_dpcm_fe' + - 'for_each_drhd_unit' + - 'for_each_dss_dev' + - 'for_each_efi_memory_desc' + - 'for_each_efi_memory_desc_in_map' + - 'for_each_element' + - 'for_each_element_extid' + - 'for_each_element_id' + - 'for_each_endpoint_of_node' + - 'for_each_evictable_lru' + - 'for_each_fib6_node_rt_rcu' + - 'for_each_fib6_walker_rt' + - 'for_each_free_mem_pfn_range_in_zone' + - 'for_each_free_mem_pfn_range_in_zone_from' + - 'for_each_free_mem_range' + - 'for_each_free_mem_range_reverse' + - 'for_each_func_rsrc' + - 'for_each_hstate' + - 'for_each_if' + - 'for_each_iommu' + - 'for_each_ip_tunnel_rcu' + - 'for_each_irq_nr' + - 'for_each_link_codecs' + - 'for_each_link_cpus' + - 'for_each_link_platforms' + - 'for_each_lru' + - 'for_each_matching_node' + - 'for_each_matching_node_and_match' + - 'for_each_member' + - 'for_each_memcg_cache_index' + - 'for_each_mem_pfn_range' + - '__for_each_mem_range' + - 'for_each_mem_range' + - '__for_each_mem_range_rev' + - 'for_each_mem_range_rev' + - 'for_each_mem_region' + - 'for_each_migratetype_order' + - 'for_each_msi_entry' + - 'for_each_msi_entry_safe' + - 'for_each_net' + - 'for_each_net_continue_reverse' + - 'for_each_netdev' + - 'for_each_netdev_continue' + - 'for_each_netdev_continue_rcu' + - 'for_each_netdev_continue_reverse' + - 'for_each_netdev_feature' + - 'for_each_netdev_in_bond_rcu' + - 'for_each_netdev_rcu' + - 'for_each_netdev_reverse' + - 'for_each_netdev_safe' + - 'for_each_net_rcu' + - 'for_each_new_connector_in_state' + - 'for_each_new_crtc_in_state' + - 'for_each_new_mst_mgr_in_state' + - 'for_each_new_plane_in_state' + - 'for_each_new_private_obj_in_state' + - 'for_each_node' + - 'for_each_node_by_name' + - 'for_each_node_by_type' + - 'for_each_node_mask' + - 'for_each_node_state' + - 'for_each_node_with_cpus' + - 'for_each_node_with_property' + - 'for_each_nonreserved_multicast_dest_pgid' + - 'for_each_of_allnodes' + - 'for_each_of_allnodes_from' + - 'for_each_of_cpu_node' + - 'for_each_of_pci_range' + - 'for_each_old_connector_in_state' + - 'for_each_old_crtc_in_state' + - 'for_each_old_mst_mgr_in_state' + - 'for_each_oldnew_connector_in_state' + - 'for_each_oldnew_crtc_in_state' + - 'for_each_oldnew_mst_mgr_in_state' + - 'for_each_oldnew_plane_in_state' + - 'for_each_oldnew_plane_in_state_reverse' + - 'for_each_oldnew_private_obj_in_state' + - 'for_each_old_plane_in_state' + - 'for_each_old_private_obj_in_state' + - 'for_each_online_cpu' + - 'for_each_online_node' + - 'for_each_online_pgdat' + - 'for_each_pci_bridge' + - 'for_each_pci_dev' + - 'for_each_pci_msi_entry' + - 'for_each_pcm_streams' + - 'for_each_physmem_range' + - 'for_each_populated_zone' + - 'for_each_possible_cpu' + - 'for_each_present_cpu' + - 'for_each_prime_number' + - 'for_each_prime_number_from' + - 'for_each_process' + - 'for_each_process_thread' + - 'for_each_property_of_node' + - 'for_each_registered_fb' + - 'for_each_requested_gpio' + - 'for_each_requested_gpio_in_range' + - 'for_each_reserved_mem_range' + - 'for_each_reserved_mem_region' + - 'for_each_rtd_codec_dais' + - 'for_each_rtd_components' + - 'for_each_rtd_cpu_dais' + - 'for_each_rtd_dais' + - 'for_each_set_bit' + - 'for_each_set_bit_from' + - 'for_each_set_clump8' + - 'for_each_sg' + - 'for_each_sg_dma_page' + - 'for_each_sg_page' + - 'for_each_sgtable_dma_page' + - 'for_each_sgtable_dma_sg' + - 'for_each_sgtable_page' + - 'for_each_sgtable_sg' + - 'for_each_sibling_event' + - 'for_each_subelement' + - 'for_each_subelement_extid' + - 'for_each_subelement_id' + - '__for_each_thread' + - 'for_each_thread' + - 'for_each_unicast_dest_pgid' + - 'for_each_vsi' + - 'for_each_wakeup_source' + - 'for_each_zone' + - 'for_each_zone_zonelist' + - 'for_each_zone_zonelist_nodemask' + - 'fwnode_for_each_available_child_node' + - 'fwnode_for_each_child_node' + - 'fwnode_graph_for_each_endpoint' + - 'gadget_for_each_ep' + - 'genradix_for_each' + - 'genradix_for_each_from' + - 'hash_for_each' + - 'hash_for_each_possible' + - 'hash_for_each_possible_rcu' + - 'hash_for_each_possible_rcu_notrace' + - 'hash_for_each_possible_safe' + - 'hash_for_each_rcu' + - 'hash_for_each_safe' + - 'hctx_for_each_ctx' + - 'hlist_bl_for_each_entry' + - 'hlist_bl_for_each_entry_rcu' + - 'hlist_bl_for_each_entry_safe' + - 'hlist_for_each' + - 'hlist_for_each_entry' + - 'hlist_for_each_entry_continue' + - 'hlist_for_each_entry_continue_rcu' + - 'hlist_for_each_entry_continue_rcu_bh' + - 'hlist_for_each_entry_from' + - 'hlist_for_each_entry_from_rcu' + - 'hlist_for_each_entry_rcu' + - 'hlist_for_each_entry_rcu_bh' + - 'hlist_for_each_entry_rcu_notrace' + - 'hlist_for_each_entry_safe' + - 'hlist_for_each_entry_srcu' + - '__hlist_for_each_rcu' + - 'hlist_for_each_safe' + - 'hlist_nulls_for_each_entry' + - 'hlist_nulls_for_each_entry_from' + - 'hlist_nulls_for_each_entry_rcu' + - 'hlist_nulls_for_each_entry_safe' + - 'i3c_bus_for_each_i2cdev' + - 'i3c_bus_for_each_i3cdev' + - 'ide_host_for_each_port' + - 'ide_port_for_each_dev' + - 'ide_port_for_each_present_dev' + - 'idr_for_each_entry' + - 'idr_for_each_entry_continue' + - 'idr_for_each_entry_continue_ul' + - 'idr_for_each_entry_ul' + - 'in_dev_for_each_ifa_rcu' + - 'in_dev_for_each_ifa_rtnl' + - 'inet_bind_bucket_for_each' + - 'inet_lhash2_for_each_icsk_rcu' + - 'key_for_each' + - 'key_for_each_safe' + - 'klp_for_each_func' + - 'klp_for_each_func_safe' + - 'klp_for_each_func_static' + - 'klp_for_each_object' + - 'klp_for_each_object_safe' + - 'klp_for_each_object_static' + - 'kunit_suite_for_each_test_case' + - 'kvm_for_each_memslot' + - 'kvm_for_each_vcpu' + - 'list_for_each' + - 'list_for_each_codec' + - 'list_for_each_codec_safe' + - 'list_for_each_continue' + - 'list_for_each_entry' + - 'list_for_each_entry_continue' + - 'list_for_each_entry_continue_rcu' + - 'list_for_each_entry_continue_reverse' + - 'list_for_each_entry_from' + - 'list_for_each_entry_from_rcu' + - 'list_for_each_entry_from_reverse' + - 'list_for_each_entry_lockless' + - 'list_for_each_entry_rcu' + - 'list_for_each_entry_reverse' + - 'list_for_each_entry_safe' + - 'list_for_each_entry_safe_continue' + - 'list_for_each_entry_safe_from' + - 'list_for_each_entry_safe_reverse' + - 'list_for_each_entry_srcu' + - 'list_for_each_prev' + - 'list_for_each_prev_safe' + - 'list_for_each_safe' + - 'llist_for_each' + - 'llist_for_each_entry' + - 'llist_for_each_entry_safe' + - 'llist_for_each_safe' + - 'mci_for_each_dimm' + - 'media_device_for_each_entity' + - 'media_device_for_each_intf' + - 'media_device_for_each_link' + - 'media_device_for_each_pad' + - 'nanddev_io_for_each_page' + - 'netdev_for_each_lower_dev' + - 'netdev_for_each_lower_private' + - 'netdev_for_each_lower_private_rcu' + - 'netdev_for_each_mc_addr' + - 'netdev_for_each_uc_addr' + - 'netdev_for_each_upper_dev_rcu' + - 'netdev_hw_addr_list_for_each' + - 'nft_rule_for_each_expr' + - 'nla_for_each_attr' + - 'nla_for_each_nested' + - 'nlmsg_for_each_attr' + - 'nlmsg_for_each_msg' + - 'nr_neigh_for_each' + - 'nr_neigh_for_each_safe' + - 'nr_node_for_each' + - 'nr_node_for_each_safe' + - 'of_for_each_phandle' + - 'of_property_for_each_string' + - 'of_property_for_each_u32' + - 'pci_bus_for_each_resource' + - 'pcl_for_each_chunk' + - 'pcl_for_each_segment' + - 'pcm_for_each_format' + - 'ping_portaddr_for_each_entry' + - 'plist_for_each' + - 'plist_for_each_continue' + - 'plist_for_each_entry' + - 'plist_for_each_entry_continue' + - 'plist_for_each_entry_safe' + - 'plist_for_each_safe' + - 'pnp_for_each_card' + - 'pnp_for_each_dev' + - 'protocol_for_each_card' + - 'protocol_for_each_dev' + - 'queue_for_each_hw_ctx' + - 'radix_tree_for_each_slot' + - 'radix_tree_for_each_tagged' + - 'rbtree_postorder_for_each_entry_safe' + - 'rdma_for_each_block' + - 'rdma_for_each_port' + - 'rdma_umem_for_each_dma_block' + - 'resource_list_for_each_entry' + - 'resource_list_for_each_entry_safe' + - 'rhl_for_each_entry_rcu' + - 'rhl_for_each_rcu' + - 'rht_for_each' + - 'rht_for_each_entry' + - 'rht_for_each_entry_from' + - 'rht_for_each_entry_rcu' + - 'rht_for_each_entry_rcu_from' + - 'rht_for_each_entry_safe' + - 'rht_for_each_from' + - 'rht_for_each_rcu' + - 'rht_for_each_rcu_from' + - '__rq_for_each_bio' + - 'rq_for_each_bvec' + - 'rq_for_each_segment' + - 'scsi_for_each_prot_sg' + - 'scsi_for_each_sg' + - 'sctp_for_each_hentry' + - 'sctp_skb_for_each' + - 'shdma_for_each_chan' + - '__shost_for_each_device' + - 'shost_for_each_device' + - 'sk_for_each' + - 'sk_for_each_bound' + - 'sk_for_each_entry_offset_rcu' + - 'sk_for_each_from' + - 'sk_for_each_rcu' + - 'sk_for_each_safe' + - 'sk_nulls_for_each' + - 'sk_nulls_for_each_from' + - 'sk_nulls_for_each_rcu' + - 'snd_array_for_each' + - 'snd_pcm_group_for_each_entry' + - 'snd_soc_dapm_widget_for_each_path' + - 'snd_soc_dapm_widget_for_each_path_safe' + - 'snd_soc_dapm_widget_for_each_sink_path' + - 'snd_soc_dapm_widget_for_each_source_path' + - 'tb_property_for_each' + - 'tcf_exts_for_each_action' + - 'udp_portaddr_for_each_entry' + - 'udp_portaddr_for_each_entry_rcu' + - 'usb_hub_for_each_child' + - 'v4l2_device_for_each_subdev' + - 'v4l2_m2m_for_each_dst_buf' + - 'v4l2_m2m_for_each_dst_buf_safe' + - 'v4l2_m2m_for_each_src_buf' + - 'v4l2_m2m_for_each_src_buf_safe' + - 'virtio_device_for_each_vq' + - 'while_for_each_ftrace_op' + - 'xa_for_each' + - 'xa_for_each_marked' + - 'xa_for_each_range' + - 'xa_for_each_start' + - 'xas_for_each' + - 'xas_for_each_conflict' + - 'xas_for_each_marked' + - 'xbc_array_for_each_value' + - 'xbc_for_each_key_value' + - 'xbc_node_for_each_array_value' + - 'xbc_node_for_each_child' + - 'xbc_node_for_each_key_value' + - 'zorro_for_each_dev' + +IncludeBlocks: Preserve # Unknown to clang-format-5.0 +IncludeCategories: + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None # Unknown to clang-format-5.0 +IndentWidth: 8 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto # Unknown to clang-format-5.0 +ObjCBlockIndentWidth: 8 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true + +# Taken from git's rules +PenaltyBreakAssignment: 10 # Unknown to clang-format-4.0 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +PointerAlignment: Right +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false # Unknown to clang-format-4.0 +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCtorInitializerColon: true # Unknown to clang-format-5.0 +SpaceBeforeInheritanceColon: true # Unknown to clang-format-5.0 +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true # Unknown to clang-format-5.0 +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp03 +TabWidth: 8 +UseTab: false \ No newline at end of file diff --git a/Program2/witharray/Makefile b/Program2/witharray/Makefile new file mode 100644 index 0000000..fdc65ba --- /dev/null +++ b/Program2/witharray/Makefile @@ -0,0 +1,7 @@ +all: witharray + +witharray: witharray.c + gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o witharray witharray.c + +clean: + rm -rf witharray diff --git a/Program2/witharray/notes.c b/Program2/witharray/notes.c new file mode 100644 index 0000000..e69de29 diff --git a/Program2/witharray/notes.md b/Program2/witharray/notes.md new file mode 100644 index 0000000..9807db9 --- /dev/null +++ b/Program2/witharray/notes.md @@ -0,0 +1,12 @@ +# CS 491 Lecture 5 + +## Shared Memory + +- Permissions + - 0666 Gives everyone rw access to the memory +- Detatching + - Need to detatch the shared memory or it will persist. +- Tools + - ipcs + - Shows shared memory segments and owners + - t \ No newline at end of file diff --git a/Program2/witharray/witharray.c b/Program2/witharray/witharray.c new file mode 100644 index 0000000..05082ef --- /dev/null +++ b/Program2/witharray/witharray.c @@ -0,0 +1,274 @@ +// Ziad Arafat +// Created: sep 18 2021 +// Figure 1.10 in the textbook + +/** + * @brief This program will create an array of a specified size and fill it + * with random numbers between 1 and 3 inclusive. It will then try to count + * the number of threes in the array using a serial method and a parallel method + * it will return the counts for each attempt as well as the running times. + * + */ + +/** +Changelog +09/17 +- Copied code from the withmutex.c code +- added dynamic array allocation for the results of the threads +- removed the mutex lock and unlock +- modified the thread to indrement the corresponding item in the array instead of global count + + +09/19 +- added free() calls to clean up the memory when done + +09/21 +- fixed the way the array is split for the threads + + +*/ + +#define DEBUG 0 // flag to print debug values + +#include +#include +#include +#include + +int *A; // Pointer to our array in heap +int COUNT = 0; // The count of threes we found for the parallel code +int SEGSIZE; // the size of each chunk per tread +int NUMOFTHREADS; // the number of threads to spawn +int *t_results; // an array to store the results from each thread +int SIZE; // The size of the array + +pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable + +/** + * @brief Iterates through a chunk of the A array and counts the number of + * threes by updating an item in a results array corresponding to the thread. + * + * + * @param index The index of the array chunks re are working with + * @return void* returns a void pointer to work with pthread + * + * @pre The global A variable must be a pointer to an array in heap containing numbers between + * 1 and 3. index must be a number referring to the index of the chunk we want + * to work through starting at 0. The global t_results array must be 0 at the + * corresponding index + * + * @post When done the array element corresponding to the thread must have the + * number of threes we found in our chunk of the array + */ +void *count3s(void *idx) +{ + int *index = (int *)idx; // cast the void star input to an int ptr + int mystart = *index * SEGSIZE; // set the starting point for the thread + int myend = mystart + SEGSIZE; // set the end point for the thread + + if (DEBUG) { + printf("I ran once %d\n", *index); + } + + if (DEBUG) { + printf("start is %d\n", mystart); + printf("end is %d\n", myend); + } + + // iterate through our chunk and increment our array element when a + // three is found + for (int i = mystart; i < myend; i++) { + if (A[i] == 3) { + t_results[*index]++; // increment result + } + } + + // printf("Threads %d index %d size %d END %d \n", NUMOFTHREADS, *index, SIZE, myend); + + // When we reach the final index we will calculate the remaining values in the array + // So that we can add them to the result. + // This can become quite high once you go past the 50% mark of the array size + // but our tests will never go that far so this is a pretty good solution. + if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) { + if (DEBUG) { + int remain = SIZE - myend; + printf("Remainder: %d \n", remain); + } + + // iterate through the remainder of the array and count the 3s + for (int i = myend; i < SIZE; i++) { + if (A[i] == 3) { + t_results[*index]++; + } + } + } + + return (void *)0; +} + +/** + * @brief This function is used to spawn the appropriate number of threads and + * also gathers the results of each thread and sums them. + * + * @return int containing the time it took + * + * @pre Global COUNT is 0. NUMOFTHREADS is greater than 0. + * SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable + * points to an array in heap. + * + * @post The global COUNT contains the correct three count + * + */ +int count3s_parallel() +{ + // initialize the timer for the parallel work + struct timespec starttime, endtime; + int j, k; + + // get the start time + clock_gettime(CLOCK_REALTIME, &starttime); // start the timer + + // Created an array allocated at runtime to hold the identifiers for + // our threads + pthread_t *t_idents; + int *t_indices; + + // Allocates the space needed for the thread IDs + t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS)); + + // in order to fix the "integer to pointer conversion" warning + // i made an array to store the indices for each thread that way + // i can pass it as a pointer in the for loop + t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS)); + + // allocate an array of ints to store the results. + t_results = (int *)(calloc(NUMOFTHREADS, sizeof(int) * NUMOFTHREADS)); + + // Create the threads + for (int i = 0; i < NUMOFTHREADS; i++) { + t_indices[i] = i; + pthread_create(&t_idents[i], NULL, count3s, + (void *)&t_indices[i]); + } + + // wait for all the treads to finish. + for (int i = 0; i < NUMOFTHREADS; i++) { + pthread_join(t_idents[i], NULL); + COUNT += t_results[i]; + } + + // free the three arrays we made so we dont leak. + free(t_idents); + free(t_indices); + free(t_results); + + // get the end time + clock_gettime(CLOCK_REALTIME, &endtime); + + // calculate duration + j = endtime.tv_sec - starttime.tv_sec; + k = endtime.tv_nsec - starttime.tv_nsec; + j = j * 1000000000 + k; + + // return the duration + return j; +} + +int main(int argc, char const *argv[]) +{ + // this variable stores the time for generating the random numbers. + time_t t; + + // intitilize the random number with the time as the seed + srand((unsigned)time(&t)); + + // check the user arguments + if (argc != 3) { + // if they are wrong count barf + printf("Not correct arguments \n"); + printf("Usage %s r of threads> \n", + argv[0]); + exit(1); + } + + // Convert the user input to integers + SIZE = atoi(argv[1]); + NUMOFTHREADS = atoi(argv[2]); + + // Set the size of the array chunks to process to the size over the + // number of threads + SEGSIZE = SIZE / NUMOFTHREADS; + + if (DEBUG) { + printf("number is %s \n", argv[1]); + printf("number is %d \n", SIZE); + printf("NumThr is %d \n", NUMOFTHREADS); + printf("Seg Size is %d \n", SEGSIZE); + } + + // Allocate the array based on the input size + A = (int *)(malloc(sizeof(int) * SIZE)); + + // if the array is null barf + if (A == NULL) { + // printf("Austin, we have a problem"); + perror("Austin, we have a problem"); + exit(1); + } + + // generate random numbers and fill them in the array + // the numbers are between 1 and 3 inclusive + for (int i = 0; i < SIZE; i++) { + // if we are debugging lets make them all threes. + // that way we can tell if something isnt + // being counted. + if (DEBUG) { + A[i] = 3; + } else { + A[i] = rand() % 4; + } + } + + // Count the threes in parallel + int parallel_time = count3s_parallel(); + + // since the COUNT for the parallel is defined before starting the + // timer i choose to define this one before timer as well. + int local_count = 0; + + // initialize the timer for the parallel work + struct timespec starttime, endtime; + int j, k; + clock_gettime(CLOCK_REALTIME, &starttime); // start the timer + + // Count the threes in serial + for (int i = 0; i < SIZE; i++) { + if (A[i] == 3) { + local_count++; + } + } + + // get end time + clock_gettime(CLOCK_REALTIME, &endtime); + + // calculate duration + j = endtime.tv_sec - starttime.tv_sec; + k = endtime.tv_nsec - starttime.tv_nsec; + j = j * 1000000000 + k; + + // free the array + free(A); + + // print the times and counts + printf("Time parallel: %d \n", parallel_time); + printf("Time serial: %d \n", j); + + printf("parallel count is %d\n", COUNT); + printf("serial count is %d\n", local_count); + + if (DEBUG) { + printf("SEGSIZE %d \n", SEGSIZE); + } + + return 0; +} diff --git a/Program2/withmutex/.clang-format b/Program2/withmutex/.clang-format new file mode 100644 index 0000000..77cecdd --- /dev/null +++ b/Program2/withmutex/.clang-format @@ -0,0 +1,550 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# clang-format configuration file. Intended for clang-format >= 4. +# +# For more information, see: +# +# Documentation/process/clang-format.rst +# https://clang.llvm.org/docs/ClangFormat.html +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +# +--- +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left # Unknown to clang-format-4.0 +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false # Unknown to clang-format-5.0 + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true # Unknown to clang-format-4.0 + SplitEmptyRecord: true # Unknown to clang-format-4.0 + SplitEmptyNamespace: true # Unknown to clang-format-4.0 +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false # Unknown to clang-format-4.0 +BreakBeforeTernaryOperators: false +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeComma # Unknown to clang-format-4.0 +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false # Unknown to clang-format-4.0 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false # Unknown to clang-format-4.0 + +# Taken from: +# git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' include/ \ +# | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$, - '\1'," \ +# | sort | uniq +ForEachMacros: + - 'apei_estatus_for_each_section' + - 'ata_for_each_dev' + - 'ata_for_each_link' + - '__ata_qc_for_each' + - 'ata_qc_for_each' + - 'ata_qc_for_each_raw' + - 'ata_qc_for_each_with_internal' + - 'ax25_for_each' + - 'ax25_uid_for_each' + - '__bio_for_each_bvec' + - 'bio_for_each_bvec' + - 'bio_for_each_bvec_all' + - 'bio_for_each_integrity_vec' + - '__bio_for_each_segment' + - 'bio_for_each_segment' + - 'bio_for_each_segment_all' + - 'bio_list_for_each' + - 'bip_for_each_vec' + - 'bitmap_for_each_clear_region' + - 'bitmap_for_each_set_region' + - 'blkg_for_each_descendant_post' + - 'blkg_for_each_descendant_pre' + - 'blk_queue_for_each_rl' + - 'bond_for_each_slave' + - 'bond_for_each_slave_rcu' + - 'bpf_for_each_spilled_reg' + - 'btree_for_each_safe128' + - 'btree_for_each_safe32' + - 'btree_for_each_safe64' + - 'btree_for_each_safel' + - 'card_for_each_dev' + - 'cgroup_taskset_for_each' + - 'cgroup_taskset_for_each_leader' + - 'cpufreq_for_each_entry' + - 'cpufreq_for_each_entry_idx' + - 'cpufreq_for_each_valid_entry' + - 'cpufreq_for_each_valid_entry_idx' + - 'css_for_each_child' + - 'css_for_each_descendant_post' + - 'css_for_each_descendant_pre' + - 'device_for_each_child_node' + - 'dma_fence_chain_for_each' + - 'do_for_each_ftrace_op' + - 'drm_atomic_crtc_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane' + - 'drm_atomic_crtc_state_for_each_plane_state' + - 'drm_atomic_for_each_plane_damage' + - 'drm_client_for_each_connector_iter' + - 'drm_client_for_each_modeset' + - 'drm_connector_for_each_possible_encoder' + - 'drm_for_each_bridge_in_chain' + - 'drm_for_each_connector_iter' + - 'drm_for_each_crtc' + - 'drm_for_each_crtc_reverse' + - 'drm_for_each_encoder' + - 'drm_for_each_encoder_mask' + - 'drm_for_each_fb' + - 'drm_for_each_legacy_plane' + - 'drm_for_each_plane' + - 'drm_for_each_plane_mask' + - 'drm_for_each_privobj' + - 'drm_mm_for_each_hole' + - 'drm_mm_for_each_node' + - 'drm_mm_for_each_node_in_range' + - 'drm_mm_for_each_node_safe' + - 'flow_action_for_each' + - 'for_each_active_dev_scope' + - 'for_each_active_drhd_unit' + - 'for_each_active_iommu' + - 'for_each_aggr_pgid' + - 'for_each_available_child_of_node' + - 'for_each_bio' + - 'for_each_board_func_rsrc' + - 'for_each_bvec' + - 'for_each_card_auxs' + - 'for_each_card_auxs_safe' + - 'for_each_card_components' + - 'for_each_card_dapms' + - 'for_each_card_pre_auxs' + - 'for_each_card_prelinks' + - 'for_each_card_rtds' + - 'for_each_card_rtds_safe' + - 'for_each_card_widgets' + - 'for_each_card_widgets_safe' + - 'for_each_cgroup_storage_type' + - 'for_each_child_of_node' + - 'for_each_clear_bit' + - 'for_each_clear_bit_from' + - 'for_each_cmsghdr' + - 'for_each_compatible_node' + - 'for_each_component_dais' + - 'for_each_component_dais_safe' + - 'for_each_comp_order' + - 'for_each_console' + - 'for_each_cpu' + - 'for_each_cpu_and' + - 'for_each_cpu_not' + - 'for_each_cpu_wrap' + - 'for_each_dapm_widgets' + - 'for_each_dev_addr' + - 'for_each_dev_scope' + - 'for_each_displayid_db' + - 'for_each_dma_cap_mask' + - 'for_each_dpcm_be' + - 'for_each_dpcm_be_rollback' + - 'for_each_dpcm_be_safe' + - 'for_each_dpcm_fe' + - 'for_each_drhd_unit' + - 'for_each_dss_dev' + - 'for_each_efi_memory_desc' + - 'for_each_efi_memory_desc_in_map' + - 'for_each_element' + - 'for_each_element_extid' + - 'for_each_element_id' + - 'for_each_endpoint_of_node' + - 'for_each_evictable_lru' + - 'for_each_fib6_node_rt_rcu' + - 'for_each_fib6_walker_rt' + - 'for_each_free_mem_pfn_range_in_zone' + - 'for_each_free_mem_pfn_range_in_zone_from' + - 'for_each_free_mem_range' + - 'for_each_free_mem_range_reverse' + - 'for_each_func_rsrc' + - 'for_each_hstate' + - 'for_each_if' + - 'for_each_iommu' + - 'for_each_ip_tunnel_rcu' + - 'for_each_irq_nr' + - 'for_each_link_codecs' + - 'for_each_link_cpus' + - 'for_each_link_platforms' + - 'for_each_lru' + - 'for_each_matching_node' + - 'for_each_matching_node_and_match' + - 'for_each_member' + - 'for_each_memcg_cache_index' + - 'for_each_mem_pfn_range' + - '__for_each_mem_range' + - 'for_each_mem_range' + - '__for_each_mem_range_rev' + - 'for_each_mem_range_rev' + - 'for_each_mem_region' + - 'for_each_migratetype_order' + - 'for_each_msi_entry' + - 'for_each_msi_entry_safe' + - 'for_each_net' + - 'for_each_net_continue_reverse' + - 'for_each_netdev' + - 'for_each_netdev_continue' + - 'for_each_netdev_continue_rcu' + - 'for_each_netdev_continue_reverse' + - 'for_each_netdev_feature' + - 'for_each_netdev_in_bond_rcu' + - 'for_each_netdev_rcu' + - 'for_each_netdev_reverse' + - 'for_each_netdev_safe' + - 'for_each_net_rcu' + - 'for_each_new_connector_in_state' + - 'for_each_new_crtc_in_state' + - 'for_each_new_mst_mgr_in_state' + - 'for_each_new_plane_in_state' + - 'for_each_new_private_obj_in_state' + - 'for_each_node' + - 'for_each_node_by_name' + - 'for_each_node_by_type' + - 'for_each_node_mask' + - 'for_each_node_state' + - 'for_each_node_with_cpus' + - 'for_each_node_with_property' + - 'for_each_nonreserved_multicast_dest_pgid' + - 'for_each_of_allnodes' + - 'for_each_of_allnodes_from' + - 'for_each_of_cpu_node' + - 'for_each_of_pci_range' + - 'for_each_old_connector_in_state' + - 'for_each_old_crtc_in_state' + - 'for_each_old_mst_mgr_in_state' + - 'for_each_oldnew_connector_in_state' + - 'for_each_oldnew_crtc_in_state' + - 'for_each_oldnew_mst_mgr_in_state' + - 'for_each_oldnew_plane_in_state' + - 'for_each_oldnew_plane_in_state_reverse' + - 'for_each_oldnew_private_obj_in_state' + - 'for_each_old_plane_in_state' + - 'for_each_old_private_obj_in_state' + - 'for_each_online_cpu' + - 'for_each_online_node' + - 'for_each_online_pgdat' + - 'for_each_pci_bridge' + - 'for_each_pci_dev' + - 'for_each_pci_msi_entry' + - 'for_each_pcm_streams' + - 'for_each_physmem_range' + - 'for_each_populated_zone' + - 'for_each_possible_cpu' + - 'for_each_present_cpu' + - 'for_each_prime_number' + - 'for_each_prime_number_from' + - 'for_each_process' + - 'for_each_process_thread' + - 'for_each_property_of_node' + - 'for_each_registered_fb' + - 'for_each_requested_gpio' + - 'for_each_requested_gpio_in_range' + - 'for_each_reserved_mem_range' + - 'for_each_reserved_mem_region' + - 'for_each_rtd_codec_dais' + - 'for_each_rtd_components' + - 'for_each_rtd_cpu_dais' + - 'for_each_rtd_dais' + - 'for_each_set_bit' + - 'for_each_set_bit_from' + - 'for_each_set_clump8' + - 'for_each_sg' + - 'for_each_sg_dma_page' + - 'for_each_sg_page' + - 'for_each_sgtable_dma_page' + - 'for_each_sgtable_dma_sg' + - 'for_each_sgtable_page' + - 'for_each_sgtable_sg' + - 'for_each_sibling_event' + - 'for_each_subelement' + - 'for_each_subelement_extid' + - 'for_each_subelement_id' + - '__for_each_thread' + - 'for_each_thread' + - 'for_each_unicast_dest_pgid' + - 'for_each_vsi' + - 'for_each_wakeup_source' + - 'for_each_zone' + - 'for_each_zone_zonelist' + - 'for_each_zone_zonelist_nodemask' + - 'fwnode_for_each_available_child_node' + - 'fwnode_for_each_child_node' + - 'fwnode_graph_for_each_endpoint' + - 'gadget_for_each_ep' + - 'genradix_for_each' + - 'genradix_for_each_from' + - 'hash_for_each' + - 'hash_for_each_possible' + - 'hash_for_each_possible_rcu' + - 'hash_for_each_possible_rcu_notrace' + - 'hash_for_each_possible_safe' + - 'hash_for_each_rcu' + - 'hash_for_each_safe' + - 'hctx_for_each_ctx' + - 'hlist_bl_for_each_entry' + - 'hlist_bl_for_each_entry_rcu' + - 'hlist_bl_for_each_entry_safe' + - 'hlist_for_each' + - 'hlist_for_each_entry' + - 'hlist_for_each_entry_continue' + - 'hlist_for_each_entry_continue_rcu' + - 'hlist_for_each_entry_continue_rcu_bh' + - 'hlist_for_each_entry_from' + - 'hlist_for_each_entry_from_rcu' + - 'hlist_for_each_entry_rcu' + - 'hlist_for_each_entry_rcu_bh' + - 'hlist_for_each_entry_rcu_notrace' + - 'hlist_for_each_entry_safe' + - 'hlist_for_each_entry_srcu' + - '__hlist_for_each_rcu' + - 'hlist_for_each_safe' + - 'hlist_nulls_for_each_entry' + - 'hlist_nulls_for_each_entry_from' + - 'hlist_nulls_for_each_entry_rcu' + - 'hlist_nulls_for_each_entry_safe' + - 'i3c_bus_for_each_i2cdev' + - 'i3c_bus_for_each_i3cdev' + - 'ide_host_for_each_port' + - 'ide_port_for_each_dev' + - 'ide_port_for_each_present_dev' + - 'idr_for_each_entry' + - 'idr_for_each_entry_continue' + - 'idr_for_each_entry_continue_ul' + - 'idr_for_each_entry_ul' + - 'in_dev_for_each_ifa_rcu' + - 'in_dev_for_each_ifa_rtnl' + - 'inet_bind_bucket_for_each' + - 'inet_lhash2_for_each_icsk_rcu' + - 'key_for_each' + - 'key_for_each_safe' + - 'klp_for_each_func' + - 'klp_for_each_func_safe' + - 'klp_for_each_func_static' + - 'klp_for_each_object' + - 'klp_for_each_object_safe' + - 'klp_for_each_object_static' + - 'kunit_suite_for_each_test_case' + - 'kvm_for_each_memslot' + - 'kvm_for_each_vcpu' + - 'list_for_each' + - 'list_for_each_codec' + - 'list_for_each_codec_safe' + - 'list_for_each_continue' + - 'list_for_each_entry' + - 'list_for_each_entry_continue' + - 'list_for_each_entry_continue_rcu' + - 'list_for_each_entry_continue_reverse' + - 'list_for_each_entry_from' + - 'list_for_each_entry_from_rcu' + - 'list_for_each_entry_from_reverse' + - 'list_for_each_entry_lockless' + - 'list_for_each_entry_rcu' + - 'list_for_each_entry_reverse' + - 'list_for_each_entry_safe' + - 'list_for_each_entry_safe_continue' + - 'list_for_each_entry_safe_from' + - 'list_for_each_entry_safe_reverse' + - 'list_for_each_entry_srcu' + - 'list_for_each_prev' + - 'list_for_each_prev_safe' + - 'list_for_each_safe' + - 'llist_for_each' + - 'llist_for_each_entry' + - 'llist_for_each_entry_safe' + - 'llist_for_each_safe' + - 'mci_for_each_dimm' + - 'media_device_for_each_entity' + - 'media_device_for_each_intf' + - 'media_device_for_each_link' + - 'media_device_for_each_pad' + - 'nanddev_io_for_each_page' + - 'netdev_for_each_lower_dev' + - 'netdev_for_each_lower_private' + - 'netdev_for_each_lower_private_rcu' + - 'netdev_for_each_mc_addr' + - 'netdev_for_each_uc_addr' + - 'netdev_for_each_upper_dev_rcu' + - 'netdev_hw_addr_list_for_each' + - 'nft_rule_for_each_expr' + - 'nla_for_each_attr' + - 'nla_for_each_nested' + - 'nlmsg_for_each_attr' + - 'nlmsg_for_each_msg' + - 'nr_neigh_for_each' + - 'nr_neigh_for_each_safe' + - 'nr_node_for_each' + - 'nr_node_for_each_safe' + - 'of_for_each_phandle' + - 'of_property_for_each_string' + - 'of_property_for_each_u32' + - 'pci_bus_for_each_resource' + - 'pcl_for_each_chunk' + - 'pcl_for_each_segment' + - 'pcm_for_each_format' + - 'ping_portaddr_for_each_entry' + - 'plist_for_each' + - 'plist_for_each_continue' + - 'plist_for_each_entry' + - 'plist_for_each_entry_continue' + - 'plist_for_each_entry_safe' + - 'plist_for_each_safe' + - 'pnp_for_each_card' + - 'pnp_for_each_dev' + - 'protocol_for_each_card' + - 'protocol_for_each_dev' + - 'queue_for_each_hw_ctx' + - 'radix_tree_for_each_slot' + - 'radix_tree_for_each_tagged' + - 'rbtree_postorder_for_each_entry_safe' + - 'rdma_for_each_block' + - 'rdma_for_each_port' + - 'rdma_umem_for_each_dma_block' + - 'resource_list_for_each_entry' + - 'resource_list_for_each_entry_safe' + - 'rhl_for_each_entry_rcu' + - 'rhl_for_each_rcu' + - 'rht_for_each' + - 'rht_for_each_entry' + - 'rht_for_each_entry_from' + - 'rht_for_each_entry_rcu' + - 'rht_for_each_entry_rcu_from' + - 'rht_for_each_entry_safe' + - 'rht_for_each_from' + - 'rht_for_each_rcu' + - 'rht_for_each_rcu_from' + - '__rq_for_each_bio' + - 'rq_for_each_bvec' + - 'rq_for_each_segment' + - 'scsi_for_each_prot_sg' + - 'scsi_for_each_sg' + - 'sctp_for_each_hentry' + - 'sctp_skb_for_each' + - 'shdma_for_each_chan' + - '__shost_for_each_device' + - 'shost_for_each_device' + - 'sk_for_each' + - 'sk_for_each_bound' + - 'sk_for_each_entry_offset_rcu' + - 'sk_for_each_from' + - 'sk_for_each_rcu' + - 'sk_for_each_safe' + - 'sk_nulls_for_each' + - 'sk_nulls_for_each_from' + - 'sk_nulls_for_each_rcu' + - 'snd_array_for_each' + - 'snd_pcm_group_for_each_entry' + - 'snd_soc_dapm_widget_for_each_path' + - 'snd_soc_dapm_widget_for_each_path_safe' + - 'snd_soc_dapm_widget_for_each_sink_path' + - 'snd_soc_dapm_widget_for_each_source_path' + - 'tb_property_for_each' + - 'tcf_exts_for_each_action' + - 'udp_portaddr_for_each_entry' + - 'udp_portaddr_for_each_entry_rcu' + - 'usb_hub_for_each_child' + - 'v4l2_device_for_each_subdev' + - 'v4l2_m2m_for_each_dst_buf' + - 'v4l2_m2m_for_each_dst_buf_safe' + - 'v4l2_m2m_for_each_src_buf' + - 'v4l2_m2m_for_each_src_buf_safe' + - 'virtio_device_for_each_vq' + - 'while_for_each_ftrace_op' + - 'xa_for_each' + - 'xa_for_each_marked' + - 'xa_for_each_range' + - 'xa_for_each_start' + - 'xas_for_each' + - 'xas_for_each_conflict' + - 'xas_for_each_marked' + - 'xbc_array_for_each_value' + - 'xbc_for_each_key_value' + - 'xbc_node_for_each_array_value' + - 'xbc_node_for_each_child' + - 'xbc_node_for_each_key_value' + - 'zorro_for_each_dev' + +IncludeBlocks: Preserve # Unknown to clang-format-5.0 +IncludeCategories: + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None # Unknown to clang-format-5.0 +IndentWidth: 8 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto # Unknown to clang-format-5.0 +ObjCBlockIndentWidth: 8 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true + +# Taken from git's rules +PenaltyBreakAssignment: 10 # Unknown to clang-format-4.0 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +PointerAlignment: Right +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false # Unknown to clang-format-4.0 +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCtorInitializerColon: true # Unknown to clang-format-5.0 +SpaceBeforeInheritanceColon: true # Unknown to clang-format-5.0 +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true # Unknown to clang-format-5.0 +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp03 +TabWidth: 8 +UseTab: false \ No newline at end of file diff --git a/Program2/withmutex/Makefile b/Program2/withmutex/Makefile new file mode 100644 index 0000000..894b009 --- /dev/null +++ b/Program2/withmutex/Makefile @@ -0,0 +1,7 @@ +all: withmutex + +withmutex: withmutex.c + gcc -O0 -Wall -Werror -Wextra -pedantic -pthread -lpthread -o withmutex withmutex.c + +clean: + rm -rf withmutex diff --git a/Program2/withmutex/notes.c b/Program2/withmutex/notes.c new file mode 100644 index 0000000..e69de29 diff --git a/Program2/withmutex/notes.md b/Program2/withmutex/notes.md new file mode 100644 index 0000000..9807db9 --- /dev/null +++ b/Program2/withmutex/notes.md @@ -0,0 +1,12 @@ +# CS 491 Lecture 5 + +## Shared Memory + +- Permissions + - 0666 Gives everyone rw access to the memory +- Detatching + - Need to detatch the shared memory or it will persist. +- Tools + - ipcs + - Shows shared memory segments and owners + - t \ No newline at end of file diff --git a/Program2/withmutex/withmutex.c b/Program2/withmutex/withmutex.c new file mode 100644 index 0000000..bb10a73 --- /dev/null +++ b/Program2/withmutex/withmutex.c @@ -0,0 +1,288 @@ +// Ziad Arafat +// Created: Sep 17 2021 +// Figure 1.9 in the textbook + +/** + * @brief This program will create an array of a specified size and fill it + * with random numbers between 1 and 3 inclusive. It will then try to count + * the number of threes in the array using a serial method and a parallel method + * it will return the counts for each attempt as well as the running times. + * + * + * This version adds a mutex to prevent the race condition. We expect the + * the outputs to be correct but the performance will be very bad because + * the threads will all be waiting on each other and locking/unlocking the + * mutex causing an overhead with the OS. + */ + +/** +Changelog + +09/17 +- Copied code from the figure1_7.c +- added mutux initializer +- added lock and unlock to the thread function + + +09/19 +- added free() calls to clean up the memory when done + +09/21 +- fixed the way the array is split for the threads + + +*/ + +#define DEBUG 0 // flag to print debug values + +#include +#include +#include +#include + +int *A; // Pointer to our array in heap +int COUNT = 0; // The count of threes we found for the parallel code +int SEGSIZE; // the size of each chunk per tread +int NUMOFTHREADS; // the number of threads to spawn +int SIZE; // The size of the input array + +pthread_mutex_t mtex = PTHREAD_MUTEX_INITIALIZER; // this is our mutex variable + +/** + * @brief Iterates through a chunk of the A array and counts the number of + * threes by updating the COUNT variable + * + * + * @param index The index of the array chunks re are working with + * @return void* returns a void pointer to work with pthread + * + * @pre The global A variable must be a pointer to an array in heap containing numbers between + * 1 and 3. index must be a number referring to the index of the chunk we want + * to work through starting at 0. The global COUNT variable must contain the + * number of threes we have already counted + * + * @post When done the COUNT variable must be updated to have the number of + * threes found in our chunk of the array + */ +void *count3s(void *idx) +{ + // if (DEBUG) + // printf("I ran once %d\n", index); + + int *index = (int *)idx; // cast the void star input to an int ptr + int mystart = *index * SEGSIZE; // set the starting point for the thread + int myend = mystart + SEGSIZE; // set the end point for the thread + + if (DEBUG) { + printf("start is %d\n", mystart); + printf("end is %d\n", myend); + } + + // iterate through our section of the array and count the threes + for (int i = mystart; i < myend; i++) { + if (A[i] == 3) { + if (DEBUG) { + printf("Found a three\n"); + } + + // lock the mutex thereby waiting until other threads + // are done with this section of the code before + // locking it. + pthread_mutex_lock(&mtex); // lock the mutex + + COUNT++; // increment count + + // unlock the mutex to let the OS know we are done + // with this section of code. + pthread_mutex_unlock(&mtex); + } + } + + // When we reach the final index we will calculate the remaining values in the array + // So that we can add them to the result. + // This can become quite high once you go past the 50% mark of the array size + // but our tests will never go that far so this is a pretty good solution. + if ((myend < SIZE) && (*index == NUMOFTHREADS - 1)) { + if (DEBUG) { + int remain = SIZE - myend; + printf("Remainder: %d \n", remain); + } + + // iterate through the remainder of the array and count the 3s + for (int i = myend; i < SIZE; i++) { + // if its a three in the array + if (A[i] == 3) { + // lock the mutex thereby waiting until other threads + // are done with this section of the code before + // locking it. + pthread_mutex_lock(&mtex); // lock the mutex + + COUNT++; // increment count + + // unlock the mutex to let the OS know we are done + // with this section of code. + pthread_mutex_unlock(&mtex); + } + } + + if (DEBUG) { + printf("The count is now %d threads %d index %d SIZE %d end %d \n", + COUNT, NUMOFTHREADS, *index, SIZE, myend); + } + } + + return (void *)0; +} + +/** + * @brief This function is used to spawn the appropriate number of threads + * + * @return int containing the time it took + * + * @pre The global COUNT variable is 0. NUMOFTHREADS is greater than 0. + * SEGSIZE is the size of the array divided by NUMOFTHREADS. the A variable + * points to an array in heap. + * + * @post The global COUNT contains the correct three count + * + */ +int count3s_parallel() +{ + // initialize the timer for the parallel work + struct timespec starttime, endtime; + int j, k; + + // get the start time + clock_gettime(CLOCK_REALTIME, &starttime); // start the timer + + // Created an array allocated at runtime to hold the identifiers for + // our threads + pthread_t *t_idents; + int *t_indices; + + // Allocates the space needed for the thread IDs + t_idents = (pthread_t *)(malloc(sizeof(pthread_t) * NUMOFTHREADS)); + + // in order to fix the "integer to pointer conversion" warning + // i made an array to store the indices for each thread that way + // i can pass it as a pointer in the for loop + t_indices = (int *)(malloc(sizeof(int) * NUMOFTHREADS)); + + // Create the threads + for (int i = 0; i < NUMOFTHREADS; i++) { + t_indices[i] = i; + pthread_create(&t_idents[i], NULL, count3s, + (void *)&t_indices[i]); + } + + // wait for all the treads to finish. + for (int i = 0; i < NUMOFTHREADS; i++) { + pthread_join(t_idents[i], NULL); + } + + // free the ids and indexes in memory so we dont leak + free(t_idents); + free(t_indices); + + // get the end time + clock_gettime(CLOCK_REALTIME, &endtime); + + // calculate duration + j = endtime.tv_sec - starttime.tv_sec; + k = endtime.tv_nsec - starttime.tv_nsec; + j = j * 1000000000 + k; + + // return the duration + return j; +} + +int main(int argc, char const *argv[]) +{ + // this variable stores the time for generating the random numbers. + time_t t; + + // intitilize the random number with the time as the seed + srand((unsigned)time(&t)); + + + // check the user arguments + if (argc != 3) { + // if they are wrong count barf + printf("Not correct arguments \n"); + printf("Usage %s r of threads> \n", + argv[0]); + exit(1); + } + + // Convert the user input to integers + SIZE = atoi(argv[1]); + NUMOFTHREADS = atoi(argv[2]); + + // Set the size of the array chunks to process to the size over the + // number of threads + SEGSIZE = SIZE / NUMOFTHREADS; + + if (DEBUG) { + printf("number is %s \n", argv[1]); + printf("number is %d \n", SIZE); + printf("NumThr is %d \n", NUMOFTHREADS); + printf("Seg Size is %d \n", SEGSIZE); + } + + // Allocate the array based on the input size + A = (int *)(malloc(sizeof(int) * SIZE)); + + // if the array is null barf + if (A == NULL) { + perror("Austin, we have a problem"); + // printf("Austin, we have a problem"); + exit(1); + } + + // generate random numbers and fill them in the array + // the numbers are between 1 and 3 inclusive + for (int i = 0; i < SIZE; i++) { + A[i] = rand() % 4; + + // printf("E%d \n", A[i]); + } + + // Count the threes in parallel + int parallel_time = count3s_parallel(); + + // since the COUNT for the parallel is defined before starting the + // timer i choose to define this one before timer as well. + int local_count = 0; + + // initialize the timer for the parallel work + struct timespec starttime, endtime; + int j, k; + clock_gettime(CLOCK_REALTIME, &starttime); // start the timer + + // Count the threes in serial + for (int i = 0; i < SIZE; i++) { + if (A[i] == 3) { + local_count++; + } + } + + // get end time + clock_gettime(CLOCK_REALTIME, &endtime); + + // calculate duration + j = endtime.tv_sec - starttime.tv_sec; + k = endtime.tv_nsec - starttime.tv_nsec; + j = j * 1000000000 + k; + + // free the array + free(A); + + // print the times and counts + printf("Time parallel: %d \n", parallel_time); + printf("Time serial: %d \n", j); + + printf("parallel count is %d\n", COUNT); + printf("serial count is %d\n", local_count); + + return 0; +} diff --git a/README.md b/README.md index 8a0c1aa..0dd8fed 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,7 @@ These are the programs and lab reports i did for Cooper's Parallel Programming c - First program demonstrating how threads behave depending on how they access the memory - Program1_5 (Program 1.5) - Small test to demonstrate if semaphores will work with forked processes. +- Program2 + - Has the same structure and tests ar program 1 but does it with shared memory as well. +-